admin管理员组文章数量:1310508
I am trying to add Timestamp
in Firestore document on Firebase Cloud functions.
I had tried firestore.Timestamp.fromDate(new Date())
, but its not working.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firestore = admin.firestore();
const createdAt = firestore.Timestamp.fromDate(new Date());
console.log(createdAt);
Value of createdAt
should return the server timestamp, but its throwing error.
I am trying to add Timestamp
in Firestore document on Firebase Cloud functions.
I had tried firestore.Timestamp.fromDate(new Date())
, but its not working.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firestore = admin.firestore();
const createdAt = firestore.Timestamp.fromDate(new Date());
console.log(createdAt);
Value of createdAt
should return the server timestamp, but its throwing error.
4 Answers
Reset to default 7Instead of this:
const firestore = admin.firestore();
const createdAt = firestore.Timestamp.fromDate(new Date());
Try this:
const firestore = admin.firestore;
const createdAt = firestore.Timestamp.fromDate(new Date());
Might be hard to spot the difference. So here is the explanation: You are calling the function "firestore()" of the object "admin" instead of accessing the property "firestore" of the object "admin" which has the property "Timestamp" you are looking for.
This line of code has a syntax error in it. It's not valid JavaScript:
const createdAt: firestore.Timestamp.fromDate(new Date());
It looks like you meant to write this:
const createdAt = firestore.Timestamp.fromDate(new Date());
I personallly used const timestamp = new Date().getTime();
then saved it to the firstore document and it worked.
When using cloud function hooks specifically functions.auth.user().onCreate((user) => {})
I use the user.metadata.creationTime
to get the exact creation time without having to go into timezone and date/time issues.
本文标签: javascriptHow to add Timestamp in firebase cloud functionsStack Overflow
版权声明:本文标题:javascript - How to add Timestamp in firebase cloud functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741822237a2399432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论