简体   繁体   中英

NodeJs TimeStamp not setting up in correct format Firebase-Firestore

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",}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM