gpt4 book ai didi

firebase - 检测到与预期实例不匹配的 "Timestamp"类型的对象

转载 作者:行者123 更新时间:2023-12-04 23:12:40 28 4
gpt4 key购买 nike

我想知道为什么 Timestamp 对象没有按我的预期工作?

它在测试环境中工作(我使用 Mocha),但在部署时抛出错误。

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()
}

堆栈跟踪
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"
}

最佳答案

现在我明白为什么它会抛出错误了。因为我单独导入 Firestore 对象,而我应该只使用 Firebase Admin SDK 中的 Firestore 对象。

我改变了什么:

  • 中删除“@google-cloud/firestore”依赖项package.json
  • 使用 admin.firestore.Timestamp 对象。

  • 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()
    }

    关于firebase - 检测到与预期实例不匹配的 "Timestamp"类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52608930/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com