我正在尝试在添加新消息时通过Firebase Cloud Functions向组的每个成员发送推送通知。我不习惯使用Promises,在这里需要一些帮助。
exports.sendPushFromMessage = functions.database.ref('chats/{groupId}/messages/{msgId}').onCreate((snap, context) => {
const groupId = context.params.groupId;
const users = [];
const msgTitle = 'New Message';
const msgBody = snap.val().messageText;
const creator = snap.val().from;
const root = snap.ref.root;
let messages = [];
return root.child(`groups/${groupId}/selected`).once('value', snapshot => {
snapshot.forEach(user => {
root.child(`users/${user.val().uid}`).once('value', spanshot => {
const expoToken = snapshot.val().token;
const userId = snapshot.val().uid;
if (expoToken && (creator !== userId)) {
messages.push({
"to": expoToken,
"title": msgTitle,
"body": msgBody
});
}
})
})
return messages;
}).then(messages => {
return fetch('https://exp.host/--/api/v2/push/send', {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(messages)
})
}).catch((e) => console.log(e));
})
在Firebase函数的日志中,我看到以下错误:
函数返回了未定义的预期承诺或值
我知道问题可能在于我如何处理Promises。这个问题困扰了我很长一段时间,如果有人可以帮助我,那真是太棒了。
这是我尝试过的另一种方法:
exports.sendPushFromMessage = functions.database.ref('chats/{groupId}/messages/{msgId}').onCreate((snap, context) => {
const groupId = context.params.groupId;
const users = [];
const msgTitle = 'New Message';
const msgBody = snap.val().messageText;
const creator = snap.val().from;
const root = snap.ref.root;
root.child(`groups/${groupId}/selected`).once('value', snapshot => {
let users = [];
snapshot.forEach(user => {
const uid = user.val().uid;
users.push(uid);
console.log('fotze' + user.val().uid);
})
return Promise.all(users);
})
.then(users => {
let messages = [];
console.log(users);
users.forEach(user => {
console.log('fucking bitch: ' + user.toString());
console.log('bitch!' + user);
root.child(`users/${user}/`).once('value', snapshot => {
const expoToken = snapshot.val().token;
const userId = snapshot.val().uid;
console.log('cunt' + snapshot.val().token);
if (expoToken && (creator !== userId)) {
messages.push({
"to": expoToken,
"title": msgTitle,
"body": msgBody
})
} // if
}); // once
}); //forEach
return Promise.all(messages);
})
.then(messages => {
return fetch('https://exp.host/--/api/v2/push/send', {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(messages)
})
}).catch((e) => console.log(e));
})
答案 0 :(得分:1)
请以这种方式进行测试:
exports.sendPushFromMessage = functions.database.ref('chats/{groupId}/messages/{msgId}').onCreate((snap, context) => {
const groupId = context.params.groupId;
const users = [];
const msgTitle = 'New Message';
const msgBody = snap.val().messageText;
const creator = snap.val().from;
const root = snap.ref.root;
let messages = [];
const promises = [];
return root.child(`groups/${groupId}/selected`).once('value', snapshot => {
snapshot.forEach(user => {
const promise = root.child(`users/${user.val().uid}`).once('value', snapshot => {
const expoToken = snapshot.val().token;
const userId = snapshot.val().uid;
if (expoToken && (creator !== userId)) {
messages.push({
"to": expoToken,
"title": msgTitle,
"body": msgBody
});
}
});
promises.push(promise);
});
return Promise.all(promises).then(() => {
return sendMessages(messages);
});
});
});
function sendMessages(messages){
return new Promise((resolve, reject) => {
const f = fetch('https://exp.host/--/api/v2/push/send', {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(messages)
});
f.on("end", () => {
resolve();
});
});
}