[英]Firebase.firestore() is not a function?
We are trying to convert this json object timestamp:我们正在尝试转换此 json object 时间戳:
Object {
"_nanoseconds": 725000000,
"_seconds": 1621386976,
}
to a firebase timestamp:到 firebase 时间戳:
t {
"nanoseconds": 725000000,
"seconds": 1621386976,
}
Our code where the error is being thrown:我们抛出错误的代码:
const lastItemIndex = thoughts.length - 1;
console.log(thoughts[lastItemIndex].date_created); // <--- logs Object timestamp
const seconds = thoughts[lastItemIndex].date_created._seconds;
console.log(seconds); // <--- logs seconds
const nanoseconds = thoughts[lastItemIndex].date_created._nanoseconds;
console.log(nanoseconds); // <---- logs nanoseconds
const lastDate = Firebase.firestore().Timestamp(seconds, nanoseconds); // <--- error
console.log(lastDate);
We are importing Firebase in our file like so:我们在我们的文件中导入 Firebase,如下所示:
import Firebase from '../../firebase';
And within the firebase.js file:在 firebase.js 文件中:
import * as firebase from 'firebase/app';
// Optionally import the services that you want to use
import 'firebase/firestore';
The warning we get:我们得到的警告:
[Unhandled promise rejection: TypeError: _firebase.default.firestore().Timestamp is not a function. (In '_firebase.default.firestore().Timestamp(seconds, nanoseconds)', '_firebase.default.firestore().Timestamp' is undefined)]
We have also tried the following:我们还尝试了以下方法:
const lastDate = new Firebase.firestore.Timestamp(seconds, nanoseconds);
and get the following error:并得到以下错误:
[Unhandled promise rejection: TypeError: undefined is not a constructor (evaluating 'new _firebase.default.firestore.Timestamp(seconds, nanoseconds)')]
We are following the docs to no avail.我们遵循文档无济于事。 How can we convert this correctly?我们如何正确转换它?
Edit编辑
exporting both Time_stamp and Firebase breaks the app [ the rest of the app does not recognize the Firebase export ]同时导出 Time_stamp 和 Firebase 会破坏应用程序 [应用程序的 rest 无法识别 Firebase 导出]
export default Firebase
makes everything back to normal. export default Firebase
使一切恢复正常。 But the issue of converting the timestamp still remains但是转换时间戳的问题仍然存在
// Initialize Firebase
export const Firebase = firebase.initializeApp(firebaseConfig);
export const Time_stamp = firebase.firestore.Timestamp();
// export default Firebase;
The problem lies in how you are importing & exporting the library.问题在于您如何导入和导出库。
If this is where you are importing from the main library, you also need to make sure you are exporting it correctly.如果这是您从主库导入的位置,您还需要确保正确导出它。 Looking at your current firebase.js
file:查看您当前的firebase.js
文件:
import * as firebase from 'firebase/app';
// Optionally import the services that you want to use
import 'firebase/firestore';
/* ... */
// Initialize Firebase
const Firebase = firebase.initializeApp(firebaseConfig);
export default Firebase; // <- this is a firebase.app.App not firebase itself
You are exporting an instance of firebase.app.App
instead of firebase
(the whole firebase library & namespace).您正在导出firebase.app.App
的实例,而不是firebase
(整个 firebase 库和命名空间)。
When you have an firebase.app.App
instance, you can access Firestore of that app using app.firestore()
.当您拥有firebase.app.App
实例时,您可以使用app.firestore()
访问该应用的Firestore。 Because you import this app instance as Firebase
in your main code, you confuse this with the normal firebase.firestore()
which does something else.因为您在主代码中将此应用程序实例导入为Firebase
,所以您会将其与执行其他操作的普通firebase.firestore()
混淆。
To help illustrate the difference, take a look at this:为了帮助说明差异,请看一下:
import * as firebase from "firebase/app";
import "firebase/firestore";
const config = { /* ... */ };
const defaultFirebaseApp = firebase.initializeApp(config);
// the instance of Firestore for the default app
const dbApp = defaultFirebaseApp.firestore();
// the instance of Firestore for the default app
const dbDefault = firebase.firestore();
// OR
// const dbDefault = firebase.firestore(dbApp);
console.log(dbApp === dbDefault); // -> true
const namedFirebaseApp = firebase.initializeApp(config, "something");
const dbNamedApp = namedFirebaseApp.firestore(); // returns instance of Firestore for the named app
// OR
// const dbNamedApp = firebase.firestore(dbNamedApp);
console.log(dbDefault === dbNamedApp); // -> false
To properly export the Firebase library from firebase.js
, you need to (and should) be using:要从firebase.js
正确导出 Firebase 库,您需要(并且应该)使用:
import firebase from 'firebase/app';
import 'firebase/firestore';
/* ... */
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase; // re-export firebase library & namespace
By re-exporting the library this way, you can use it in the same way as all the code samples you encounter:通过以这种方式重新导出库,您可以像遇到的所有代码示例一样使用它:
import firebase from '../../firebase';
const {_nanoseconds, _seconds} = thoughts[lastItemIndex].date_created;
const dateCreatedAsTimestamp = new firebase.firestore.Timestamp(_nanoseconds, _seconds);
const db = firebase.firestore();
db.doc("collection/doc")
.set({
date_created: dateCreatedAsTimestamp
})
.then(
() => console.log("success"),
(err) => console.error("failed", err);
);
If you intend to add some utility functions to firebase.js
, the way you import stuff changes slightly如果您打算在firebase.js
中添加一些实用功能,则导入内容的方式会略有变化
import firebase from 'firebase/app';
import 'firebase/firestore';
/* ... */
// Initialize Firebase
export const defaultApp = firebase.initializeApp(firebaseConfig);
export function castToTimestamp(timestampLikeObject) {
const {_nanoseconds, _seconds} = timestampLikeObject;
return new firebase.firestore.Timestamp(_nanoseconds, _seconds);
}
export default firebase; // re-export firebase library & namespace as the default
With the above file, you would instead import it as:使用上述文件,您可以将其导入为:
// you can import this normally like in the other example, but we'll
// include some of the other exports (like the utility function)
import firebase, { castToTimestamp } from '../../firebase';
const {_nanoseconds, _seconds} = thoughts[lastItemIndex].date_created;
const dateCreatedAsTimestamp = new firebase.firestore.Timestamp(_nanoseconds, _seconds);
// OR
// const dateCreatedAsTimestamp = castToTimestamp(thoughts[lastItemIndex].date_created);
const db = firebase.firestore();
db.doc("collection/doc")
.set({
date_created: dateCreatedAsTimestamp
})
.then(
() => console.log("success"),
(err) => console.error("failed", err);
);
The following works:以下作品:
export const timestamp = firebase.firestore.Timestamp;
let bob = new timestamp();
console.log("bob, bob);
NOTE:笔记:
firebase.firestore.Timestamp() firebase.firestore.Timestamp()
NOT不是
firebase.firestore().Timestamp() firebase.firestore().Timestamp()
https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.