【问题标题】:Inconsistent results from Firestore Cloud FunctionsFirestore Cloud Functions 的结果不一致
【发布时间】:2018-04-23 16:38:46
【问题描述】:

我在 Firebase 上设置了 Cloud Function,其中涉及检查 Firestore 数据库的不同部分,然后通过 Cloud Messaging 发送消息

以下是相关函数的 JavaScript:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().Firebase);
var db = admin.firestore();
exports.newMemberNotification = functions.firestore
.document('Teams/{teamId}/Waitlist/{userId}').onDelete((snap, context) => {
  // get the user we want to send the message to
  const newValue = snap.data();
  const teamidno = context.params.teamId;
  const useridno = newValue.userID;

  //start retrieving Waitlist user's messaging token to send them a message
  var tokenRef = db.collection('Users').doc(useridno);
  tokenRef.get()
  .then(doc => {
    if (!doc.exists) {
      console.log('No such document!');
    } else {
      const data = doc.data();
      //get the messaging token
      var token = data.messaging_token;
      console.log("token: ", token);
      //reference for the members collection
      var memberRef = db.collection('Teams/'+teamidno+'    /Members').doc(useridno);
      memberRef.get()
      .then(doc => {
        if (!doc.exists){
          console.log('user was not added to team. Informing them');
          const negPayload = {
            data: {
              data_type:"team_rejection",
              title:"Request denied",
              message: "Your request to join the team has been denied",
            }
          };
          return admin.messaging().sendToDevice(token, negPayload)
          .then(function(response){
            console.log("Successfully sent rejection message:", response);
            return 0;
          })
          .catch(function(error){
            console.log("Error sending rejection message: ", error);
          });
        } else {
          console.log('user was added to the team. Informing them')
          const payload = {
            data: {
              data_type: "team_accept",
              title: "Request approved",
              message: "You have been added to the team",
            }
          };
          return admin.messaging().sendToDevice(token, payload)
          .then(function(response){
            console.log("Successfully sent accept message:", response);
            return 0;
          })
          .catch(function(error){
            console.log("Error sending accept message: ", error);
          });
        }
      })
      .catch(err => {
        console.log('Error getting member', err);
      });
    }
    return 0;
    })
    .catch(err => {
      console.log('Error getting token', err);
    });
    return 0;
});

我遇到的问题是:

  • 代码会运行,但有时只会实际检查令牌或发送消息。
  • 函数运行时日志显示此错误:“函数返回未定义,预期的承诺或值”但根据另一个 Stack Oveflow 帖子,我添加了 return 0;处处都是 .then 结束。

我对 node.js、javascript 和 Cloud Functions 非常陌生,所以我不确定出了什么问题,或者这是否是 Firebase 的问题。您可以提供的任何帮助将不胜感激

【问题讨论】:

  • 你不能到处都返回 0。对于 Firestore 触发器(以及所有其他后台触发器),您必须返回一个在所有后台工作完成时解析的承诺。 firebase.google.com/docs/functions/database-events
  • 作为一个绝对的新手,我从心底里感谢你。我查看了 Promises 并尝试了它,当然它有效。云功能现在执行得更快,而且没有失败。

标签: javascript firebase firebase-cloud-messaging google-cloud-firestore google-cloud-functions


【解决方案1】:

正如 Doug 所说,您必须在每个“步骤”中返回一个承诺并链接这些步骤:

以下代码应该可以工作:

exports.newMemberNotification = functions.firestore
.document('Teams/{teamId}/Waitlist/{userId}').onDelete((snap, context) => {
    // get the user we want to send the message to
    const newValue = snap.data();
    const teamidno = context.params.teamId;
    const useridno = newValue.userID;

    //start retrieving Waitlist user's messaging token to send them a message
    var tokenRef = db.collection('Users').doc(useridno);
    tokenRef.get()
        .then(doc => {
            if (!doc.exists) {
                console.log('No such document!');
                throw 'No such document!';
            } else {
                const data = doc.data();
                //get the messaging token
                var token = data.messaging_token;
                console.log("token: ", token);
                //reference for the members collection
                var memberRef = db.collection('Teams/' + teamidno + '/Members').doc(useridno);
                return memberRef.get()
            }
        })
        .then(doc => {
            let payload;
            if (!doc.exists) {
                console.log('user was not added to team. Informing them');
                payload = {
                    data: {
                        data_type: "team_rejection",
                        title: "Request denied",
                        message: "Your request to join the team has been denied",
                    }
                };
            } else {
                console.log('user was added to the team. Informing them')
                payload = {
                    data: {
                        data_type: "team_accept",
                        title: "Request approved",
                        message: "You have been added to the team",
                    }
                };
            }
            return admin.messaging().sendToDevice(token, payload);
        })
        .catch(err => {
            console.log(err);
        });
});
猜你喜欢
  • 1970-01-01
  • 2018-03-23
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多