0
現在、自分のプロジェクトにfirebase関数を使用し始めました。私はGitHubのサンプルプロジェクトを見て、いくつかのサンプルプロジェクトでは、admin参照を宣言し、約束でそれらを返すときに.once("value")
を使用しました。私の問題は以下の通りです:FireBase関数.once( 'value')メソッドが未定義に戻る
私のコード:
exports.HandleNotifications = functions.database.ref('/users/{user_id}/Friends/pending/{recipient_id}').onWrite(event => {
if(!event.data.exists()){
return null;
}
console.log(event.data.val());
let recipient_id = event.data.val();
let getrecipientProfile = admin.auth().getUser(recipient_id);
let getDeviceTokens = admin.database().ref('users/'+event.params.user_id+'/deviceTokens').once('value');
let getRecipientName = admin.database().ref(`/users/${event.params.recipient_id}/displayName`).once('value');
return Promise.all([getDeviceTokens,getrecipientProfile,getRecipientName]).then(others => {
let tokensSnapshot = others[0];
let recipient = others[1];
let recipient_name = others[2];
console.log(recipient_name);
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
console.log('Fetched follower profile', recipient);
// Notification details.
const payload = {
notification: {
title: 'You have a new Friend Request!',
body: recipient.displayName + `requested to follow you.`,
icon: recipient.photoURL
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
return admin.messaging().sendToDevice(tokens,payload);
});
});
そうはconsole.log(RECIPIENT_NAME)を実行します。画像のように、次の出力を返し :
T {
A:
Rb {
B: 'StevenWong',
aa: P { k: [Object], aa: null, wb: [Object], Bb: '' },
Bb: null },
V:
R {
u:
Gd {
app: [Object],
L: [Object],
Ua: [Object],
Sc: null,
ca: [Object],
td: 1,
Qa: [Object],
va: [Object],
qg: [Object],
jc: [Object],
ee: [Object],
md: [Object],
ia: [Object],
Xa: [Object],
cd: 1,
fe: null,
K: [Object] },
path: J { o: [Object], Y: 0 },
m:
Df {
xa: false,
ka: false,
Ib: false,
na: false,
Pb: false,
oa: 0,
kb: '',
bc: null,
xb: '',
Zb: null,
'',
g: Tc {} },
Kc: false,
then: undefined,
catch: undefined },
g: Tc {} }