【问题标题】:Detected an object of type "Timestamp" that doesn't match the expected instance检测到与预期实例不匹配的“时间戳”类型的对象
【发布时间】:2019-03-07 14:45:51
【问题描述】:

我想知道为什么 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"
}

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions firebase-admin


    【解决方案1】:

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

    我改变了什么:

    1. package.json

    2. 中移除“@google-cloud/firestore”依赖项
    3. 使用 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()
    }
    

    【讨论】:

      【解决方案2】:

      当使用 firebase admin 时,避免直接导入和使用任何客户端包

      所以而不是

       import * as admin  from "firebase-admin";
       import firebase from "firebase/app";
      
       admin.firestore().collection("name").set({
             date: firebase.firestore.Timestamp.now()
      })
      

      改用这个

       import * as admin  from "firebase-admin";
      
       admin.firestore().collection("name").set({
             date: admin.firestore.Timestamp.now()
      })
      

      【讨论】:

      • 是否有一种众所周知的模式来重用使用来自公共依赖代码库的时间戳的 Firestore 代码?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多