![](/img/trans.png)
[英]How can I send and receive two types of data from one single field in firestore for flutter?
[英]In Firestore, how can I prevent "Please ensure that the Firestore types you are using are from the same NPM package" errors
我有几个使用 Firestore 的 NPM 代码库。 一个是客户端,一个是服务器端,我正在尝试将一些代码重构为一个通用的依赖代码库。 服务器代码库使用 firebase-admin 作为其依赖项,但如果我尝试设置具有哨兵类型的对象(例如 firebase.firestore.Timestamp),我会出现此错误:
Please ensure that the Firestore types you are using are from the same NPM package
我可以通过将实例注入我的库代码库来避免混合 firestore 实现,例如:
import * as admin from "firebase-admin";
const libraryCode = new MyFirestoreLibrary(admin.firestore())
但是,有没有办法在库代码中访问这些标记类型?
我希望工作的一个例子在这里: https://github.com/okhobb/firestore-dependency-tester
我同意 firebase 团队的这非常愚蠢。 我正处于您的确切位置,并且在我定义客户端和服务器之间的类型和对象的地方共享代码 - 听起来很合理吧?
这是我的解决方法:
import * as firebase from 'firebase-admin';
import Timestamp = firebase.firestore.Timestamp;
export const now = () => {
const newTimestamp = Timestamp.fromMillis(Date.now()) as any;
const ret = JSON.parse(JSON.stringify(newTimestamp));
for (const prop of Object.getOwnPropertyNames(Timestamp.prototype).filter(prop => prop !== "constructor"))
ret[prop] = newTimestamp[prop]
return ret as Timestamp;
}
这只是创建一个新的 object 并复制属性,以便 toDate、toMillis 等函数仍然存在,它不是时间戳的直接实例,因此任何 instanceof 检查都将返回 false。
在做了那个可怕的 hack 之后,他们似乎已经进行了检查以确保“您正在从同一个 npm 包导入”不再有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.