TypeError:elements.get不是firebase函数中的函数错误

时间:2019-06-18 19:12:19

标签: javascript typescript firebase google-cloud-firestore google-cloud-functions

运行firebase函数时,我得到下面的error log,我试图在recentPosts数组字段中获取文档和值。

Error: Unknown error status: Error: Unknown error status: TypeError: elements.get is not a function
    at new HttpsError (/srv/node_modules/firebase-functions/lib/providers/https.js:90:19)
    at admin.firestore.collectionGroup.where.get.then.catch.error (/srv/lib/index.js:71:15)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

课程:

class PostTable {
   public commentCount : number;

   public dateTime : number;

   public docId : string;

   public post : string;

   public userId : string;

   public userName : string;

   constructor(commentCount: number, dateTime: admin.firestore.Timestamp, docId: string, post : string, userId : string, userName : string) {

       this.commentCount = commentCount
       this.dateTime=  dateTime.toDate().getTime()
       this.docId=docId
       this.post=post
       this.userId=userId
       this.userName=userName
   }

}

功能:

export const getPosts = functions.https.onCall((data, context) => {

   if (!context.auth) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
          'while authenticated.');
    }


    let responseCollection : PostTable[] = []

         admin.firestore().collectionGroup('recentPostColl').where('users', "array-contains",context.auth.token.name)
                                 .get()

         .then(collectionOfPosts => {

            if(!collectionOfPosts.empty)
            {

               collectionOfPosts.forEach(element => {

                  let collection : Map<String, Object>[] = element.get('recentPosts')

                  collection.forEach(elements => {

                     try {
                        const p : PostTable = new PostTable(elements.get('commentCount')as number, elements.get('dateTime') as admin.firestore.Timestamp
                        ,elements.get('docId') as string,elements.get('post') as string, elements.get('userId') as string, elements.get('userName') as string);

                        const stamp : admin.firestore.Timestamp = elements.get('dateTime') as admin.firestore.Timestamp

                        const date : Date = stamp.toDate()

                        if(date.getTime() > new Date(data.date).getTime())
                        {

                              responseCollection.push(p)
                        }
                     } catch (error) {
                        throw new functions.https.HttpsError(error, 'Constructor error');
                     }

                  });
               });

            }


   })

   .catch(error =>{
      throw new functions.https.HttpsError(error, 'code error');
   })

   return responseCollection


})

文档:

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的代码中,collectionOfPostsQuerySnapshot对象。您正在使用其forEach方法来迭代其中包含的QueryDocumentSnapshot对象。然后,您使用get()从该快照中获取特定字段。该字段将被表示为纯JavaScript数组类型,因为该字段是一个数组。它不是强类型的数组-您应该假设所有字段类型都是纯JavaScript类型,而不是TypeScript集合类型。您不能简单地将普通的JavaScript类型转换为TypeScript集合类型。

您需要使用JavaScript术语提供一个关于该字段的具体,安全的断言(假设该字段完全包含您的想法):

let collection : object[] = element.get('recentPosts')

然后,您可以像collection一样索引到普通数组:

let item = collection[0]

或像其他任何JavaScript数组一样对其进行迭代。

单个项目将是object类型,您也将它们当作普通的旧JavaScript对象来处理。

如果要处理TypeScript集合类型,则必须编写一些代码以将所有这些JavaScript数组和对象转换为其等效的TypeScript集合类型。