我使用的專案@Nativescript/firebase(https://github.com/EddyVerbruggen/nativescript-plugin-firebase)忽略 的方法firebase.firestore.timestamp,并按undefined屬性回傳。
以下是最小復制
應用程式.js
import Vue from "nativescript-vue";
import Home from "./components/Home";
var firebase = require("@nativescript/firebase").firebase;
firebase
.init({})
.then(
function () {
console.log("firebase.init done");
},
function (error) {
console.log("firebase.init error: " error);
}
);
new Vue({
render: (h) => h("frame", [h(Home)]),
}).$start();
主頁.vue
import { firebase } from "@nativescript/firebase";
export default {
computed: {
async message() {
const Ref = firebase.firestore
.collection("comments")
.doc("07bhQeWDf3u1j0B4vNwG");
const doc = await Ref.get();
const hoge = doc.data();
console.log("hoge.commented_at", hoge.commented_at); // CONSOLE LOG: hoge.commented_at Sat Oct 23 2021 22:44:48 GMT 0900 (JST)
console.log("hoge.commented_at.seconds", hoge.commented_at.seconds); // CONSOLE LOG: hoge.commented_at.seconds undefined
const hogeToDate = hoge.toDate();
console.log("hogeToDate", hogeToDate); // no console.log appear
return hogeToDate; // simulator shows "object Promise"
},
},
};
我也試過const hogeTimestampNow = firebase.firestore.Timestamp.now();然后沒有console.log出現...
環境
- Vue.js
- Node.js v14.17.6
- 本機腳本 v8.1.2
- nativescript-vue v2.9.0
- @nativescript/firebase v11.1.3
uj5u.com熱心網友回復:
如果您深入研究 的來源@nativescript/firebase,特別是查看/src/firebase-common.ts,您會發現這firebase是一個自定義實作,而不是通常由普通 Firebase Web SDK 匯出的物件/命名空間。
它使用自定義實作,因此可以根據運行代碼的平臺進行轉換,如/src/firebase.android.ts和所示/src/firebase.ios.ts。
特別重要的是,Firestore 的 Timestamp 物件在向您的代碼公開時會在內部轉換為 JavaScript Date 物件,因為每個平臺都有自己的Timestamp物件版本。由于公開的 JavaScript Date 物件沒有seconds屬性,因此undefined在嘗試訪問hoge.commented_at.seconds.
該當量Timestamp#seconds將是Math.floor(hoge.commented_at / 1000)(你也可以用更明確的Math.floor(hoge.commented_at.getTime() / 1000),如果你不喜歡依賴于JavaScript的強制型別轉換)。
function getSeconds(dt: Date) {
return Math.floor(dt.getTime() / 1000)
}
雖然您可以Timestamp從 Modular Web SDK (v9 )匯入物件,但當傳遞到 NativeScript 插件時,它會變成一個普通物件(即{ seconds: number, nanoseconds: number }而不是Timestamp)。
import { Timestamp } from 'firebase/firestore/lite';
const commentedAtTS = Timestamp.fromDate(hoge.commented_at);
docRef.set({ commentedAt: commentedAtTS.toDate() }) // must turn back to Date object before writing!
uj5u.com熱心網友回復:
firebase.firestore.timestamp通過不作業@nativescript/firebase的@samthecodingman說。(https://stackoverflow.com/a/69853638/15966408)
只需使用普通的 javascript 方法并進行編輯。
我試過
- 從 firestore 獲取時間戳,然后轉換為毫秒
- 獲取日期
new Date()然后轉換為毫秒
并記錄相同的毫秒數。
通過火店
const Ref = firebase.firestore.collection("comments").doc("07bhQeWDf3u1j0B4vNwG");
const doc = await Ref.get();
const hoge = doc.data();
console.log("hoge.commented_at in milliseconds: ", Math.floor(hoge.commented_at / 1000));
// CONSOLE LOG: hoge.commented_at in milliseconds: 1634996688
通過javascript方法
const getNewDate = new Date("October 23, 2021, 22:44:48 GMT 0900");
// same as hoge.commented_at
console.log("getNewDate in milliseconds: ", getNewDate.getTime() / 1000);
// CONSOLE LOG: getNewDate in milliseconds: 1634996688
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/351479.html
標籤:火力基地 Vue.js 时间戳 本机脚本 nativescript-firebase
