Javascript 如何使用typescript中的函数从firestore获取文档?

Javascript 如何使用typescript中的函数从firestore获取文档?,javascript,typescript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Typescript,Firebase,Google Cloud Firestore,Google Cloud Functions,我想从我的收藏中获取用户信息,使用他们的ID向他们发送通知。这是我在index.ts中的函数 export const sendNotifications = functions.firestore .document('messages/{groupId1}/{groupId2}/{message}') .onCreate((snapshot, context) =>{ console.log('Starting sendNotification Function');

我想从我的收藏中获取用户信息,使用他们的ID向他们发送通知。这是我在index.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}`
    }
 }
从我的部署中,我得到控制台日志消息,“启动sendNotification函数”,然后是实际的“doc.content”,然后是我的“getUserData(doc.idFrom)”的错误

承诺{
,
域:
领域{
域:空,
_事件:{错误:[函数]},
_事件提示:1,
_maxListeners:未定义,
成员:[]}

提前谢谢你

您应该使用
wait
调用异步
getUserData()
函数

以下方法可以实现此目的(未经测试):


或者,如果不想让云函数异步,可以执行以下操作:

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

您应该使用
wait
调用异步
getUserData()
函数

以下方法可以实现此目的(未经测试):


或者,如果不想让云函数异步,可以执行以下操作:

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

如何获取{groupId2}的值?只需使用
context
对象:
context.params.groupId2
。如何获取{groupId2}的值?只需使用
context
对象:
context.params.groupId2
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;
      });
  });