My project used @Nativescript/firebase(https://github.com/EddyVerbruggen/nativescript-plugin-firebase) ignores methods of firebase.firestore.timestamp, and returns undefined by properties.
The below is minimum reproduction
app.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();
Home.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"
},
},
};
I also tried const hogeTimestampNow = firebase.firestore.Timestamp.now(); then no console.log appear...
Environment
- vue.js
- Node.js v14.17.6
- nativescript v8.1.2
- nativescript-vue v2.9.0
- @nativescript/firebase v11.1.3
If you dive into the source of
@nativescript/firebase, in particular looking at/src/firebase-common.ts, you can see thatfirebaseis a custom implementation and not the object/namespace normally exported by the ordinary Firebase Web SDK.It uses a custom implementation so that it can be transformed depending on the platform the code is running on as shown in
/src/firebase.android.tsand/src/firebase.ios.ts.Of particular importance, is that Firestore's Timestamp objects are internally converted to JavaScript Date objects when exposed to your code as each platform has its own version of a
Timestampobject. Because the exposed JavaScript Date object doesn't have asecondsproperty, you getundefinedwhen attempting to accesshoge.commented_at.seconds.The equivalent of
Timestamp#secondswould beMath.floor(hoge.commented_at / 1000)(you could also be more explicit withMath.floor(hoge.commented_at.getTime() / 1000)if you don't like relying on JavaScript's type coercion).While you can import the
Timestampobject from the Modular Web SDK (v9+), when passed into the NativeScript plugin, it would be turned into an ordinary object (i.e.{ seconds: number, nanoseconds: number }rather than aTimestamp).