TypeError:无法读取未定义的属性“ toDate”

时间:2019-06-23 19:36:14

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

我有一个firebase项目,运行firebase函数时出现以下错误日志,以下是错误日志和代码。错误是 Cannot read property 'toDate' of undefined,将admin.firestore.Timestamp转换为Date格式。如何解决这个错误

错误日志:

Unhandled error TypeError: Cannot read property 'toDate' of undefined
    at new PostTable (/srv/lib/index.js:9:34)
    at cal.then.docCollection (/srv/lib/index.js:69:39)
    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
    }
}

TypeScript:

for (let i = 0; i < posts.length; i++) {
  const p = posts[i];

  let commentCount;
  let userName;
  let docId;
  let dateTime;
  let post;
  let userId;

  for (let j = 0; j < Object.keys(p).length; j++) {
    switch (Object.keys(p)[j]) {
      case "commentCount": {
        commentCount = Object.values(p)[j];
        break;
      }

      case "userName": {
        userName = Object.values(p)[j];
        break;
      }

      case "docId": {
        docId = Object.values(p)[j];
        break;
      }

      case "dateTime": {
        dateTime = Object.values(p)[j];
        break;
      }

      case "post": {
        post = Object.values(p)[j];
        break;
      }

      case "userId": {
        userId = Object.values(p)[j];
        break;
      }
    }
  }

  const posttable: PostTable = new PostTable(
    commentCount as number,
    dateTime as admin.firestore.Timestamp,
    docId as string,
    post as string,
    userId as string,
    userName as string
  );

  const stamp: admin.firestore.Timestamp = dateTime as admin.firestore.Timestamp;

  const date: Date = stamp.toDate();

  if (date.getTime() > new Date(data.date).getTime()) {
    responseCollection.push(posttable);
  }
}

JavaScript:

class PostTable {
    constructor(commentCount, dateTime, docId, post, userId, userName) {
        this.commentCount = commentCount;
        this.dateTime = dateTime.toDate().getTime();
        this.docId = docId;
        this.post = post;
        this.userId = userId;
        this.userName = userName;
    }
}
exports.getPosts = functions.https.onCall((data, context) => {
    //const updatedDate = data.date as number
    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 = [];
    const cal = admin.firestore().collectionGroup('recentPostColl').where('users', 'array-contains', context.auth.token.name)
        .get()
        .catch(error => {
        throw new functions.https.HttpsError(error, 'code error');
    });
    return cal.then(docCollection => {
        if (docCollection.empty !== true) {
            for (const doc in docCollection.docs) {
                const document = docCollection.docs[doc];
                const posts = document.get('recentPosts');
                for (let i = 0; i < posts.length; i++) {
                    const p = posts[i];
                    let commentCount;
                    let userName;
                    let docId;
                    let dateTime;
                    let post;
                    let userId;
                    for (let j = 0; j < Object.keys(p).length; j++) {
                        switch (Object.keys(p)[j]) {
                            case "commentCount": {
                                commentCount = Object.values(p)[j];
                                break;
                            }
                            case "userName": {
                                userName = Object.values(p)[j];
                                break;
                            }
                            case "docId": {
                                docId = Object.values(p)[j];
                                break;
                            }
                            case "dateTime": {
                                dateTime = Object.values(p)[j];
                                break;
                            }
                            case "post": {
                                post = Object.values(p)[j];
                                break;
                            }
                            case "userId": {
                                userId = Object.values(p)[j];
                                break;
                            }
                        }
                    }
                    const posttable = new PostTable(commentCount, dateTime, docId, post, userId, userName);
                    const stamp = dateTime;
                    const date = stamp.toDate();
                    if (date.getTime() > new Date(data.date).getTime()) {
                        responseCollection.push(posttable);
                    }
                }
            }
        }
        return responseCollection;
    });
});

1 个答案:

答案 0 :(得分:1)

在您的班级而不是:

this.dateTime = dateTime.toDate().getTime()

this.dateTime = dateTime && dateTime.toDate().getTime()

(或更安全:this.dateTime = dateTime && dateTime.toDate && dateTime.toDate().getTime()

或者您可以使用一些默认值来解决它,例如

this.dateTime = (dateTime || defaultValue).toDate().getTime()