Reputation: 1283
I'm not getting the correct timestamp format for the firestore. From Nodejs I've Also tried a lot of others StackOverflow answers but it doesn't work at all.
The Way I'm using is here:
const Timestamp = require("firebase-firestore-timestamp");
let timeStamp = Timestamp.now();
The result on Firestore:
What I expect the result is:
The Way I'm Using:
NodeEngine=>
"engines": {
"node": "12.16.1"
},
db module=>
const firebase = require("firebase");
const config = require("./config");
const db = firebase.initializeApp(config.firebaseConfig);
module.exports = db;
my config files =>
'use strict';
const dotenv = require('dotenv');
const assert = require('assert');
dotenv.config();
const {
PORT,
HOST,
HOST_URL,
API_KEY,
AUTH_DOMAIN,
DATABASE_URL,
PROJECT_ID,
STORAGE_BUCKET,
MESSAGING_SENDER_ID,
APP_ID
} = process.env;
assert(PORT, 'PORT is required');
assert(HOST, 'HOST is required');
module.exports = {
port: PORT,
host: HOST,
url: HOST_URL,
firebaseConfig: {
apiKey: API_KEY,
authDomain: AUTH_DOMAIN,
databaseURL: DATABASE_URL,
projectId: PROJECT_ID,
storageBucket: STORAGE_BUCKET,
messagingSenderId: MESSAGING_SENDER_ID,
appId: APP_ID
}
}
controller to setup the data=>
const firebase = require("../db");
const firestore = firebase.firestore();
for setting data:
await firestore
.collection("alpha")
.doc("beta")
.set(data);
here data means a data object along with other parameters
the data:
{
level: 2,
endTime: { seconds: 1648387945, nanoseconds: 437000000 },
nextLevel:12
}
depencencies =>
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"firebase": "^8.0.1",}
Upvotes: 1
Views: 285
Reputation: 50930
You can add Timestamp to a document without using any third party packages as shown below:
const data = {
endTime: firebase.firestore.FieldValue.serverTimestamp()
// also add other fields
}
await firebase.firestore()
.collection("alpha")
.doc("beta")
.set(data);
Upvotes: 1