【发布时间】:2019-11-26 08:22:12
【问题描述】:
我正在尝试将时间戳从 Firestore 转换为 NodeJS 管理 SDK 中的 Date 对象。但我总是收到 toDate() 不是函数的错误。为什么?如何在管理 SDK 中将时间戳转换为 Date 对象?
这是我尝试过的代码。
this.getService.sample()
.then(doc => {
const callable = this.fns.httpsCallable('myTestFn');
const data$ = callable({ text: 'google', arraySize: 10, date: doc.data().date });
data$.toPromise()
.then(e1 => {
console.log(e1);
}).catch(err => {
console.log(err);
})
});
onCall 函数:
const original = data.text;
const arraySize: number = data.arraySize;
if (arraySize > 100) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('unknown', 'invalid request,input is above the permistted range!');
}
const date = data.date.toDate();
console.log('--- This is the date---', date);
const numberArray = [];
for (let i = 1; i <= arraySize; i++) {
await numberArray.push(i);
}
const sum = await numberArray.reduce((acc, curr) => acc + curr, 0);
console.log('-----------sum-------------', sum)
const writeResult = await admin.firestore().collection('messages').add({ original: original,sum, createdAt: admin.firestore.FieldValue.serverTimestamp() });
return { wish: 'A wish from function', original, writeResult: writeResult.id, numberArray, sum };
【问题讨论】:
标签: typescript google-cloud-firestore firebase-admin