Javascript Firestore云函数的结果不一致
我在Firebase上设置了一个云功能,其中包括检查Firestore数据库的不同部分,然后通过云消息发送消息 下面是有关函数的JavaScript:Javascript Firestore云函数的结果不一致,javascript,firebase,firebase-cloud-messaging,google-cloud-firestore,google-cloud-functions,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(); expor
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;
});
我在这方面遇到的问题是:
代码运行时,有时只实际检查令牌或发送消息。
当函数运行时,日志显示此错误:函数返回未定义的预期承诺或值,但根据另一个堆栈,我添加了返回0;到处都是。然后结束。
我对node.js、javascript和云函数非常陌生,所以我不确定到底出了什么问题,或者这是否是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);
});
});
你不能到处返回0。对于Firestore触发器和所有其他后台触发器,您必须返回一个承诺,该承诺在所有后台工作完成时解析。作为一个绝对的新手,我衷心感谢你。我查了一下承诺,试了试,当然成功了。云功能现在执行得更快,而且不会失败。PS:另外,我建议你看看道格关于这个主题的视频: