如何使用类型脚本中的函数从火库获取文档?

我想使用用户的 ID 从我的集合中获取用户信息,以便向他们发送通知。这是我在索引.ts中的函数


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);

    console.log(getUserData(doc.idFrom))

    return true;

});


export async function getUserData(id: string){

    try {

        const snapshot = await admin.firestore().collection('users').doc(id).get();

        const userData = snapshot.data();

        if(userData){

            return userData.nickname;

        }       


    } catch (error) {        

        console.log('Error getting User Information:', error);

        return `NOT FOUND: ${error}`

    }

 }

从我的部署中,我收到控制台日志消息,“启动发送通知函数”,然后是实际的“doc.content”,然后是我的“getUserData(doc.idFrom)”的错误。


Promise {

  <pending>,

  domain: 

   Domain {

     domain: null,

     _events: { error: [Function] },

     _eventsCount: 1,

     _maxListeners: undefined,

     members: [] } } 

提前感谢您!


偶然的你
浏览 93回答 1
1回答

宝慕林4294392

应使用 调用异步函数。getUserData()await以下应该可以解决问题(未经测试):export const sendNotifications = functions.firestore&nbsp; .document('messages/{groupId1}/{groupId2}/{message}')&nbsp; .onCreate(async (snapshot, context) => {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; console.log('Starting sendNotification Function');&nbsp; &nbsp; &nbsp; const doc = snapshot.data();&nbsp; &nbsp; &nbsp; console.log(doc.content);&nbsp; &nbsp; &nbsp; const nickname = await getUserData(doc.idFrom);&nbsp; &nbsp; &nbsp; // Do something with the nickname value&nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; } catch (error) {&nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }&nbsp; });async function getUserData(id: string) {&nbsp; try {&nbsp; &nbsp; const snapshot = await admin.firestore().collection('users').doc(id).get();&nbsp; &nbsp; if (snapshot.exists) {&nbsp; &nbsp; &nbsp; &nbsp;const userData = snapshot.data();&nbsp; &nbsp; &nbsp; &nbsp;return userData.nickname;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; //Throw an error&nbsp; &nbsp; }&nbsp; } catch (error) {&nbsp; &nbsp; // I would suggest you throw an error&nbsp; &nbsp; console.log('Error getting User Information:', error);&nbsp; &nbsp; return `NOT FOUND: ${error}`;&nbsp; }}或者,如果您不想使用云功能异步,可以执行以下操作:export const sendNotifications = functions.firestore&nbsp; .document('messages/{groupId1}/{groupId2}/{message}')&nbsp; .onCreate((snapshot, context) => {&nbsp; &nbsp; console.log('Starting sendNotification Function');&nbsp; &nbsp; const doc = snapshot.data();&nbsp; &nbsp; console.log(doc.content);&nbsp; &nbsp; return getUserData(doc.idFrom)&nbsp; &nbsp; &nbsp; .then((nickname) => {&nbsp; &nbsp; &nbsp; &nbsp; // Do something with the nickname value&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; .catch((error) => {&nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; });&nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript