Javascript Firebase Cloud函数中的变量返回;“未定义”;
我正在玩弄firebase云函数,我无法让它在没有错误的情况下运行。 它一直说userID是未定义的 以下是数据库的结构: 下面是我为函数运行的代码:Javascript Firebase Cloud函数中的变量返回;“未定义”;,javascript,node.js,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在玩弄firebase云函数,我无法让它在没有错误的情况下运行。 它一直说userID是未定义的 以下是数据库的结构: 下面是我为函数运行的代码: exports.observeMentions = functions.database.ref('/notifications/{uid}/{notificationId}/mentioned').onCreate((snapshot, context) => { var uid = context.params.uid; v
exports.observeMentions = functions.database.ref('/notifications/{uid}/{notificationId}/mentioned').onCreate((snapshot, context) => {
var uid = context.params.uid;
var notificationId = context.params.notificationId;
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/uid').once('value', snapshot => {
var userUID = snapshot.val();
return admin.database().ref('/users/' + userID).once('value', snapshot => {
var userThatMentioned = snapshot.val();
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/uid').once('value', snapshot => {
var type = snapshot.val();
return admin.database().ref('/users/' + uid).once('value', snapshot => {
var user = snapshot.val();
if ((type = 3)){
var payload = {
notification: {
body: userThatMentioned.username + ' Mentioned you in a comment'
}
};
}else{
var payload = {
notification: {
body: userThatMentioned.username + ' Mentioned you in their post'
}
};
}
if (userUID != uid){
admin.messaging().sendToDevice(user.fcmToken, payload)
.then(function(response) {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
}
})
})
})
})
})
任何帮助都将不胜感激
多谢各位
编辑/尝试:
exports.observeMentioned = functions.database.ref('/notifications/{uid}/{notificationId}/mentioned').onCreate((snapshot, context) => {
var uid = context.params.uid;
var notificationId = context.params.notificationId;
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/uid').once('value')
.then(snapshot => {
var userID = snapshot.val();
return admin.database().ref('/users/' + userID).once('value');
})
.then(snapshot => {
var userThatMentioned = snapshot.val();
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/type').once('value');
})
.then(snapshot => {
var type = snapshot.val();
return admin.database().ref('/users/' + uid).once('value');
})
.then(snapshot => {
var user = snapshot.val();
if ((type == 3)){
var payload = {
notification: {
body: userThatMentioned.username + ' Mentioned you in a comment'
}
};
}else{
var payload = {
notification: {
body: userThatMentioned.username + ' Mentioned you in their post'
}
};
}
return payload
})
.then(snapshot => {
var payload = snapshot.val();
return admin.messaging().sendToDevice(user.fcmToken, payload);
})
.catch(error => {
console.log('Error sending message:', error);
});
});
这会不断出现错误:
发送消息时出错:ReferenceError:未定义类型
我不明白为什么没有定义类型?您的代码中有几个部分需要更正:
var userUID=snapshot.val()在下一行中使用userID
(return admin.database().ref('/users/'+userID)
)=>因此出现了“未定义”错误
if
检查中,您应该使用(=
或==
)one()
方法调用then()
返回,如下所示sendToDevice()
方法返回的承诺。有关最后一点的重要性的详细信息,请参见代码中有几个部分需要更正:
var userUID=snapshot.val()在下一行中使用userID
(return admin.database().ref('/users/'+userID)
)=>因此出现了“未定义”错误
if
检查中,您应该使用(=
或==
)one()
方法调用then()
返回,如下所示sendToDevice()
方法返回的承诺。有关最后一点的重要性的详细信息,请参见我认为你有一个范围问题。 您的变量
user
、userthattheattered
和type
在链接承诺中定义,但在您需要在以后的承诺中使用它们时超出了范围
我建议在定义uid
和notificationId
的地方声明user
,userthattheattered
,并键入type
。这样,这些变量就可以在链接承诺中的任何地方访问。你甚至不必在那一点上定义它们,只要把它们举起来就行了
类似地,var payload
的声明仅在if/else语句的范围内有效。我已经纠正了这些范围问题,因此您可以看到差异
重要
从长远来看,您应该在安装的代码编辑器中考虑安装一个LITER。它会帮你解决这些问题。我的go to,强调了问题,并将其悬停在上面,将确切地告诉您发生了什么
您还绝对需要熟悉的概念,因为它是一个非常重要的组成部分编写成功的代码在任何语言
好机会
const{firestore}=require(“firebase”)
const{functions}=require(“firebase”)
const admin=firestore.admin()
exports.observemented=functions.database
.ref(“/notifications/{uid}/{notificationId}/提及”)
.onCreate((快照、上下文)=>{
var uid=context.params.uid
var notificationId=context.params.notificationId
起重机//
var类型//已吊装
已提及//已吊装
返回管理员
.数据库()
.ref(“/notifications/”+uid+“/”+notificationId+“/uid”)
.一次(“价值”)
。然后((快照)=>{
var userID=snapshot.val()
返回管理员
.数据库()
.ref(“/users/”+userID)
.一次(“价值”)
})
。然后((快照)=>{
UserThatHidded=snapshot.val()
返回管理员
.数据库()
.ref(“/notifications/”+uid+“/”+notificationId+“/type”)
.一次(“价值”)
})
。然后((快照)=>{
type=snapshot.val()
返回管理员
.数据库()
.ref(“/users/”+uid)
.一次(“价值”)
})
。然后((快照)=>{
user=snapshot.val()
吊重//
如果(类型==3){
有效载荷={
通知:{
正文:userthattiedded.username+“在评论中提到你”,
},
}
}否则{
有效载荷={
通知:{
正文:userthattheeded.username+“在他们的帖子中提到过你”,
},
}
}
返回有效负载//返回对象。。。
})
。然后((有效负载)=>{//…到下一个承诺
返回admin.messaging().sendToDevice(user.fcmToken,有效负载)
})
.catch((错误)=>{
日志(“发送消息时出错:”,错误)
})
})
我认为您存在范围界定问题。
您的变量user
、userthattheattered
和type
在链接承诺中定义,但在您需要在以后的承诺中使用它们时超出了范围
我建议在定义uid
和notificationId
的地方声明user
,userthattheattered
,并键入type
。这样,这些变量就可以在链接承诺中的任何地方访问。你甚至不必在那一点上定义它们,只要把它们举起来就行了
类似地,var payload
的声明仅在if/else语句的范围内有效。我已经纠正了这些范围问题,因此您可以看到差异
重要
从长远来看,你一定要考虑安装一个L。
exports.observeMentions = functions.database.ref('/notifications/{uid}/{notificationId}/mentioned').onCreate((snapshot, context) => {
var uid = context.params.uid;
var notificationId = context.params.notificationId;
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/uid').once('value')
.then(snapshot => {
var userID = snapshot.val();
return admin.database().ref('/users/' + userID).once('value');
})
.then(snapshot => {
var userThatMentioned = snapshot.val();
return admin.database().ref('/notifications/' + uid + '/' + notificationId + '/uid').once('value');
})
.then(snapshot => {
// ....
// return a Promise
})
.then(snapshot => {
// ....
return admin.messaging().sendToDevice(....);
})
.catch(error => {
// ...
});
});