【发布时间】:2020-07-08 03:14:06
【问题描述】:
我正在尝试编写一个 Firebase 云函数,它现在主要做两件事:
1) 每当用户在线/离线时触发
2) 获取该用户的所有对话。
以下代码能够执行步骤 1,但在控制台中返回 Function 返回 undefined、expected Promise 或 value Error。
我不确定如何编写 Promises,这是我的第一个 Firebase 函数。请帮忙。
exports.offlineHandler = functions.database.ref('/users/{userid}/status')
.onUpdate((change, context) => {
const status = change.after.val();
const userId = context.params.userid;
console.log('the user is now ', status, "with mobile no ", userId);
// fetch a users conversations list
if (status === "offline") {
console.log("offline exec start");
return fetchUserConversations(userId)
.then(results => {
console.log("offline exec end");
for (result in results) {
console.log("id is", result);
}
return results;
}).catch(err => {
console.error("An error has occurred.", err);
return err;
});
} else {
console.log("user came online");
}
return null;
});
function fetchUserConversations(userId) {
return admin.firestore().collection('users/${userId}/conversations').get()
.then(snapshot => {
var conversations = [];
snapshot.forEach(doc => {
console.log("This is conversation id => ", doc.id);
conversations.concat(doc.id);
});
return conversations;
//return conversations;
}).catch(err => {
console.error(err);
return err;
});
}
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions