Firestore云功能导致的结果不一致

时间:2018-04-23 16:38:46

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

我在Firebase上设置了云功能,其中包括检查Firestore数据库的不同部分,然后通过云消息传递发送消息

以下是相关功能的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;
});

我遇到的问题是:

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

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

1 个答案:

答案 0 :(得分:1)

正如道格所说,你必须在每个"步骤"并链接步骤:

以下代码应该有效:

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);
        });
});