1 回答
TA贡献2021条经验 获得超8个赞
应使用 调用异步函数。getUserData()await
以下应该可以解决问题(未经测试):
export const sendNotifications = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate(async (snapshot, context) => {
try {
console.log('Starting sendNotification Function');
const doc = snapshot.data();
console.log(doc.content);
const nickname = await getUserData(doc.idFrom);
// Do something with the nickname value
return true;
} catch (error) {
// ...
}
});
async function getUserData(id: string) {
try {
const snapshot = await admin.firestore().collection('users').doc(id).get();
if (snapshot.exists) {
const userData = snapshot.data();
return userData.nickname;
} else {
//Throw an error
}
} catch (error) {
// I would suggest you throw an error
console.log('Error getting User Information:', error);
return `NOT FOUND: ${error}`;
}
}
或者,如果您不想使用云功能异步,可以执行以下操作:
export const sendNotifications = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate((snapshot, context) => {
console.log('Starting sendNotification Function');
const doc = snapshot.data();
console.log(doc.content);
return getUserData(doc.idFrom)
.then((nickname) => {
// Do something with the nickname value
return true;
})
.catch((error) => {
console.log(error);
return true;
});
});
添加回答
举报