代码之家  ›  专栏  ›  技术社区  ›  Kabira Suleman

如何使用firebase云函数查询数据库

  •  1
  • Kabira Suleman  · 技术社区  · 6 年前

    database structure . “读数”字段是一个数组,每个“读数”都是一个包含字段“日期”和“值”的映射。

    目前,我可以在每次创建新用户时发送电子邮件通知,但我希望这对数据库有效。我不确定如何查询“读数”数组,然后查询每个读数。

    这是我到目前为止的代码,它在创建新用户时发送电子邮件

    exports.sendNotification = functions.auth.user().onCreate((user) => {
    
    
    const mailOptions = {
        from: '"Spammy Corp." <noreply@firebase.com>',
        to:"fakeEmail@btopenworld.com",
        text: "TEST"
    };
    
    return mailTransport.sendMail(mailOptions)
        .then(() => console.log("It worked"))
        .catch((error) => 
    console.error('There was an error while sending the email:', error)); 
    });
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Jake Lee    6 年前

    见: https://firebase.google.com/docs/firestore/extend-with-functions

    例如,要启动添加到第一个孩子的所有新阅读:

    exports.sendEmail = functions.firestore
        .document('sensor/UGt.../readings')
        .onCreate((snap, context) => {
            const newValue = snap.data();
            const value = newValue.value;
            if (value < 10) {
                // send email
            }
        });
    

    在进一步的评论中,你提到了在课堂上听新的阅读材料 source ). 相反,你将不得不听取所有人的意见 onUpdate /sensor/ ,检查更新是否正在添加读数,然后检查值&发送你的电子邮件。

    直接从添加读数的位置调用云函数可能更容易,具体取决于读取次数 由于其他原因,路径将被更新(因为每次发生这种情况都是对资源的浪费)。