TypeError:无法读取未定义的属性"toDate" [英] TypeError: Cannot read property 'toDate' of undefined

查看:120
本文介绍了TypeError:无法读取未定义的属性"toDate"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a firebase project, I am getting the below error log when running a firebase function, below are the error log and the code. the error is Cannot read property 'toDate' of undefined, converting a admin.firestore.Timestamp to Date format. How to solve this error

错误日志:

Error Log:

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

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:

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:

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

推荐答案

在您的课程中,而不是:

In your class instead of:

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

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

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

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

OR you could solve it by using some default value like

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

这篇关于TypeError:无法读取未定义的属性"toDate"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆