Javascript TypeError:无法读取属性';今天';未定义的

Javascript TypeError:无法读取属性';今天';未定义的,javascript,typescript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Typescript,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个firebase项目,当运行firebase函数时,我会得到下面的错误日志,下面是错误日志和代码。错误是 无法读取未定义的属性“toDate”,将admin.firestore.Timestamp转换为日期格式。如何解决这个错误 错误日志: 类型脚本: for(设i=0;i{ //const updateDate=data.date作为数字 如果(!context.auth){ //正在抛出HttpsError,以便客户端获取错误详细信息。 抛出新函数。https.HttpsError(

我有一个firebase项目,当运行firebase函数时,我会得到下面的错误日志,下面是错误日志和代码。错误是
无法读取未定义的属性“toDate”,将
admin.firestore.Timestamp
转换为
日期
格式。如何解决这个错误

错误日志:

类型脚本:

for(设i=0;i新日期(data.date.getTime()){
responseCollection.push(postable);
}
}
JavaScript:

类后表{
构造函数(commentCount、dateTime、docId、post、userId、用户名){
this.commentCount=commentCount;
this.dateTime=dateTime.toDate().getTime();
this.docId=docId;
this.post=post;
this.userId=userId;
this.userName=用户名;
}
}
exports.getPosts=functions.https.onCall((数据、上下文)=>{
//const updateDate=data.date作为数字
如果(!context.auth){
//正在抛出HttpsError,以便客户端获取错误详细信息。
抛出新函数。https.HttpsError('failed-predition','必须调用函数'+
“认证时。”);
}
让responseCollection=[];
const cal=admin.firestore().collectionGroup('recentPostColl')。其中('users','array contains',context.auth.token.name)
.get()
.catch(错误=>{
抛出新函数.https.HttpsError(错误,“代码错误”);
});
返回cal.then(docCollection=>{
if(docCollection.empty!==true){
for(docCollection.docs中的const doc){
const document=docCollection.docs[doc];
const posts=document.get('recentPosts');
for(设i=0;i新日期(data.date.getTime()){
responseCollection.push(postable);
}
}
}
}
返回响应采集;
});
});

在您的课堂上,而不是:

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

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

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

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


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

是否尝试对函数代码中的这一行执行相同的操作:
const-date:date=stamp.toDate()
?像这样
stamp&&stamp.toDate&&stamp.toDate()
?确实如此。这一次新错误
无法读取未定义的属性“getTime”
,但现在您可以将
if(date.getTime()>新日期(data.date).getTime()){
替换为
if(date&&date.getTime()…
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
    }
}
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);
  }
}
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;
    });
});