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 mentCount: number
public dateTime: number;
public docId: string;
public post: string;
public userId: string;
public userName: string;
constructor(mentCount: number, dateTime: admin.firestore.Timestamp, docId: string, post: string, userId: string, userName: string) {
thismentCount = mentCount
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 mentCount;
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 "mentCount": {
mentCount = 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(
mentCount 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(mentCount, dateTime, docId, post, userId, userName) {
thismentCount = mentCount;
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 mentCount;
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 "mentCount": {
mentCount = 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(mentCount, 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;
});
});
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 mentCount: number
public dateTime: number;
public docId: string;
public post: string;
public userId: string;
public userName: string;
constructor(mentCount: number, dateTime: admin.firestore.Timestamp, docId: string, post: string, userId: string, userName: string) {
this.mentCount = mentCount
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 mentCount;
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 "mentCount": {
mentCount = 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(
mentCount 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(mentCount, dateTime, docId, post, userId, userName) {
this.mentCount = mentCount;
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 mentCount;
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 "mentCount": {
mentCount = 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(mentCount, 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;
});
});
Share
Improve this question
edited Jun 24, 2019 at 10:52
VINNUSAURUS
asked Jun 23, 2019 at 19:36
VINNUSAURUSVINNUSAURUS
1,5583 gold badges19 silver badges40 bronze badges
1 Answer
Reset to default 6In your class instead of:
this.dateTime = dateTime.toDate().getTime()
do this.dateTime = dateTime && dateTime.toDate().getTime()
(or even safer: 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()