云消息错误:数据必须是非空对象



我正在构建一个云函数,当在firestore上创建文档时,该函数会发送通知。除了通知的数据参数外,一切都很好。

它产生错误:数据必须是不可为null的对象。

代码为:

exports.ReviewNotification = functions.firestore
.document("ReviewsCollection/{reviewId}")
.onCreate((snap, context) => {
const mydocument = snap.data();
const businessprofileid = mydocument.Review_bpid;
const reviewid = context.params.reviewId;
console.log(reviewid);
database.collection("BusinessProfilesCollection")
.doc(businessprofileid)
.get().then((secsnapshot) => {
const seconddocument = secsnapshot.data();
const userid = seconddocument.Profile_user_id;
const businessprofilename = seconddocument.Profile_name;
console.log(userid);
database.collection("UsersCollection")
.doc(userid)
.get().then((lastsnapshot) => {
const lastdocment = lastsnapshot.data();
const mytoken = lastdocment.User_device_token;
const data = "Type: Review, ID: "+
reviewid + ", Extra: temp";
const message ={
"notification": {
title: businessprofilename,
body: "You have new review",
},
"data": data,
"token": mytoken,
};
const response = admin.messaging().send(message);
console.log("Completed");
console.log(response);
});
});
return null;
});

有一个类似的问题,我已经检查了它的答案,建议解决方案是:

"data": {data},

这是有效的,但在应用程序上收到的消息.data如下:

{data: Type: Review, ID: vs3qxYWu5i1kQS5q8NZw, Extra: temp}

我需要它没有单词数据:,lile that:

{Type: Review, ID: vs3qxYWu5i1kQS5q8NZw, Extra: temp} 

所以在notificationonclick函数中,我可以匹配键并获取值。

问题是不能将变量放在"数据";参数。因为它可以是其他字符串。解决方案是将插入字符串方法放在下面

"data": {Type: "Review", ID: String(reviewid), Extra: String(businessprofileid)}, 

最新更新