检测到与预期实例不匹配的“时间戳”类型的对象

时间:2018-10-02 13:04:06

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

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

2 个答案:

答案 0 :(得分:5)

现在我明白为什么它会引发错误。因为我是分别导入Firestore对象的,所以我应该只使用Firebase Admin SDK中的Firestore对象。

我更改了:

  1. package.json

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

index.ts

match_parent

答案 1 :(得分:0)

使用Firebase管理员时,避免直接导入和使用任何客户端软件包

所以而不是

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