【发布时间】:2019-12-15 22:14:34
【问题描述】:
我有一个 Android 应用程序,我打算通过 fcm 实现发送通知。我已经为它编写了下面的 Javascript 代码,但是当我第一次尝试关注某人时,这些值是未定义的,但是,第二次它可以正常工作...
我尝试将这三个函数嵌套在一个函数中,还尝试在本地声明变量但未解决。
"use-strict"
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp();
var notificationDB;
var followedUserRef;
var followingUserRef;
var followingText;
var followingImageProfile;
var followedDToken_id;
var following;
var followingFullname;
exports.sendNotification =functions.database.ref("Notifications/{followed}/{notification_id}").onWrite((snapshot,context)=>{
const followed = context.params.followed;
const notification_id = context.params.notification_id;
notificationDB = admin.database().ref(`Notifications/${followed}/${notification_id}`);
console.log("Notification DB : "+notificationDB);
notificationDB.on("value",(snapshotOne)=>{
following = snapshotOne.val().userid;
followingText = snapshotOne.val().text;
});
followedUserRef = admin.database().ref(`Users/${followed}`);
followedUserRef.on("value",(snapshot)=>{
if(snapshot.val().token_id===null){
followedDToken_id = "null";
}else
followedDToken_id = snapshot.val().token_id;
});
followingUserRef = admin.database().ref(`Users/${following}`);
followingUserRef.on("value",(snapshot)=>{
followingFullname = snapshot.val().fullname;
followingImageProfile = snapshot.val().imageurl;
});
console.log("Following: "+following);
console.log("Followed: "+followed);
console.log("followed user Ref: "+followedUserRef)
console.log("NAME: "+followingFullname);
console.log("FOLLOWING TEXT: "+followingText);
console.log("tokken: "+followedDToken_id);
console.log("IMAGE: "+followingImageProfile);
});
预期的结果应该在第一次尝试时显示,但第一次失败并在第二次获取值。
【问题讨论】:
标签: javascript android node.js firebase