javascript - Firebase.firestore() 不是函数?
问题描述
我们正在尝试转换此 json 对象时间戳:
Object {
"_nanoseconds": 725000000,
"_seconds": 1621386976,
}
到firebase时间戳:
t {
"nanoseconds": 725000000,
"seconds": 1621386976,
}
我们抛出错误的代码:
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);
我们在我们的文件中导入 Firebase,如下所示:
import Firebase from '../../firebase';
在 firebase.js 文件中:
import * as firebase from 'firebase/app';
// Optionally import the services that you want to use
import 'firebase/firestore';
我们得到的警告:
[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)]
我们还尝试了以下方法:
const lastDate = new Firebase.firestore.Timestamp(seconds, nanoseconds);
并得到以下错误:
[Unhandled promise rejection: TypeError: undefined is not a constructor (evaluating 'new _firebase.default.firestore.Timestamp(seconds, nanoseconds)')]
我们遵循文档无济于事。我们如何正确转换它?
编辑
同时导出 Time_stamp 和 Firebase 会破坏应用程序 [应用程序的其余部分无法识别 Firebase 导出]
export default Firebase
让一切恢复正常。但是转换时间戳的问题仍然存在
// Initialize Firebase
export const Firebase = firebase.initializeApp(firebaseConfig);
export const Time_stamp = firebase.firestore.Timestamp();
// export default Firebase;
解决方案
问题在于您如何导入和导出库。
审查您的代码
如果这是您从主库导入的位置,您还需要确保正确导出它。查看您当前的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
您正在导出一个实例firebase.app.App
而不是firebase
(整个 firebase 库和命名空间)。
当您拥有一个firebase.app.App
实例时,您可以使用app.firestore()
. 因为您像Firebase
在主代码中一样导入此应用程序实例,所以您会将其与firebase.firestore()
执行其他操作的普通程序混淆。
为了帮助说明差异,请看一下:
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
推荐的出口风格
要从中正确导出 Firebase 库firebase.js
,您需要(并且应该)使用:
import firebase from 'firebase/app';
import 'firebase/firestore';
/* ... */
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase; // re-export firebase library & namespace
通过以这种方式重新导出库,您可以像遇到的所有代码示例一样使用它:
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);
);
替代导出样式
如果您打算向 中添加一些实用功能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
使用上述文件,您可以将其导入为:
// 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);
);
推荐阅读
- discord - 为发送的每条消息赚取积分 Bot Discord.js
- java - 如何在 JAVA 中超时后停止异步方法线程
- r - 在扩展多个 S3 类的对象上使用 S4 分派“+”
- html - 将 django 表单字段划分为两个 div
- sql-server - SSRS 表换行末尾有重复行
- python - 自 Epoc 以来,如何将 Pandas 时间戳解释为周整数
- javascript - Javascript 与地图异步
- r - 如何用空白值替换 NA?
- maven - 仅从 Maven/JFrog 存储库下载 POM 文件
- android - Android:如何确定活动是否从应用程序外部启动或恢复(进入前台)