firebase - Detected an object of type "Timestamp" that doesn't match the expected instance
问题描述
I'm wondering why is the Timestamp object is not working as I expect?
It works in test environment (I use Mocha), but throws error when it has been deployed.
index.ts
import { Timestamp, QuerySnapshot } from "@google-cloud/firestore";
....
async someFunction() {
let col = firestore.collection("mycollection");
let now = Timestamp.now();
let twentyMinsAgo = Timestamp.fromMillis(now.toMillis() - (1200 * 1000));
return col
.where('LastEdited', '>=', twentyMinsAgo) //This throws error
.get()
}
Stack Trace
Argument "value" is not a valid QueryValue.
Detected an object of type "Timestamp" that doesn't match the expected instance.
Please ensure that the Firestore types you are using are from the same NPM package.
at Validator.(anonymous function).err [as isQueryValue] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/validate.js:99:27)
at CollectionReference.where (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:940:25)
package.json
"dependencies": {
....
"@google-cloud/firestore": "^0.16.0",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.5"
}
解决方案
Now I get it why it throws an error. Because I import Firestore object separately, whereas I should just use Firestore object from Firebase Admin SDK.
What I changed:
remove "@google-cloud/firestore" dependency from package.json
Use admin.firestore.Timestamp object.
index.ts
async someFunction() {
let col = firestore.collection("mycollection");
let now = admin.firestore.Timestamp.now();
let twentyMinsAgo = admin.firestore.Timestamp.fromMillis(now.toMillis() - (1200 * 1000));
col.where('LastEdited', '>=', twentyMinsAgo) //Now ok
.get()
}
推荐阅读
- javascript - 使用 Dropzone.js 和 Laravel MediaLibrary 包上传多个文件
- android - 在可能已经在 AndroidSchedulers.mainThread() 上观察到的情况下使用 blockingGet()
- encoding - Typing Persian in `CMD`
- python - 使用不显示 object1 的 forms.ModelChoiceField 查询选择
- python - 使用python通过点拟合曲线
- javascript - Capybara attach_file 未在 Firefox 中触发 React onChange 处理程序
- entity-framework-core - 枚举为空时,AverageAsync 抛出异常
- time - 检查:两个具有不同参数和时隙的 RRULE 重叠
- reactjs - 如何使用链接设置动态参数的状态
- docusignapi - 为 MDM 异常寻找 DocuSign iOS URL 协议