Firestore云功能不一致



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

我遇到的问题是:

  • 代码运行,有时只有实际检查令牌或发送消息。
  • 当函数运行时,日志显示此错误:"函数返回未定义,预期的承诺或价值",但是根据其他堆栈Oveflow帖子,我添加了返回0;到处都是。然后结束。

我对Node.js,JavaScript和Cloud功能非常新鲜,因此我不确定出了什么问题,或者这是Firebase结束的问题。您可以提供的任何帮助将不胜感激

正如道格所说的那样,您必须在每个"步骤"上返回诺言并链接步骤:

以下代码应起作用:

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

相关内容

  • 没有找到相关文章

最新更新