检测到类型为“时间戳"的对象.与预期的实例不匹配 [英] Detected an object of type "Timestamp" that doesn't match the expected instance

查看:98
本文介绍了检测到类型为“时间戳"的对象.与预期的实例不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么Timestamp对象不能按我预期的那样工作?

I'm wondering why is the Timestamp object is not working as I expect?

它可以在测试环境中工作(我使用Mocha),但是在部署后会引发错误.

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

堆栈跟踪

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对象.

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.

我更改了:

  1. package.json

使用admin.firestore.Timestamp对象.

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

这篇关于检测到类型为“时间戳"的对象.与预期的实例不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆