首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用时间戳从防火墙获取数据

无法使用时间戳从防火墙获取数据
EN

Stack Overflow用户
提问于 2019-11-08 23:03:14
回答 1查看 639关注 0票数 4

我试图使用createdAt属性从集合中获取一个值,这是一个时间戳。我的查询大致是这样的:

代码语言:javascript
运行
复制
function getDataFromYesterdayToNow (db){
  const now = new Date()
  const yesterday = new Date(now.setDate(now.getDate() - 1))
  yesterday.setHours(0, 0, 0, 0)

  const { Timestamp } = firebase.firestore

  return db
    .collection('myData')
    .where('createdAt', '>=', Timestamp.fromDate(yesterday))
    .where('createdAt', '<=', Timestamp.fromDate(now))
    .get()
}

但是,当我运行这个程序时,我会得到以下错误:

错误是'FirebaseError: code=无效-参数:使用无效数据调用的函数Query.where()。不支持的字段值:自定义时间戳对象‘。Stacktrace是'FirebaseError:函数Query.where() --使用无效的数据调用的。

我非常困惑,我总是在其他集合中使用时间戳对象来获取,如果我只使用date对象,它就不起作用了。我是不是忘了什么?

编辑:根据要求,下面是我的文档的一个示例:

代码语言:javascript
运行
复制
{
  name: "My Data Name", // (string)
  createdAt: November 9, 2018 at 8:40:45 PM // (Timestamp)
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-16 03:51:57

我也有过同样的问题。有两件事你需要检查。

1.我是否使用了正确的SDK?

您知道可以使用两种不同的firebase吗?一个是客户端SDK (firebase-js a.k.a )。firebase包),另一个是基于防火墙的服务器SDK (nodejs-消防局 .a.k.a )。@google-cloud/firebase包)。这两个库在firestore.Timestamp类和上都有自己的实现,它们是不兼容的

其他一些NPM包依赖项如下:

代码语言:javascript
运行
复制
"@firebase/firestore" (*)
  -> "firebase" (client SDK which imports all @firebase/* except @firebase/testing)
    -> "@angular/fire" (and other client libraries with firebase binding)
    -> "@firebase/testing" (mocking Firestore client)

"@google-cloud/firebase" (*) (server SDK)
  -> "firebase-admin"
    -> "firebase-functions-test"

其中(*)表示firestore.Timestamp定义的位置。

简而言之,您应该使用相应的时间戳。

案例1.仅使用客户端SDK

代码语言:javascript
运行
复制
import { firestore, initializeApp } from 'firebase';
import { config } from './my-firebase-config';

const app = initializeApp(config);
app.firestore().collection('users')
  .where('createdAt', '<=', firestore.Timestamp.fromDate(new Date()))
  .get();

案例2.仅使用服务器SDK

代码语言:javascript
运行
复制
import { firestore, initializeApp } from 'firebase-admin';

const app = initializeApp();
app.firestore().collection('users')
  .where('createdAt', '<=', firestore.Timestamp.fromDate(new Date()))
  .get();

案例3.混合SDK

有时,在测试运行在服务器上的代码(例如,firebase函数)时,需要使用client (特别是@firebase/testing)。

代码语言:javascript
运行
复制
// server.ts
import { firestore, initializeApp } from 'firebase-admin';

const app = initializeApp();
app.firestore().collection('users')
  .where('createdAt', '<=', fs.Timestamp.fromDate(new Date()))
  .get();
代码语言:javascript
运行
复制
// server.test.ts
import { firestore } from 'firebase';
import { initializeAdminApp } from '@firebase/testing';

// Replace server sdk with client sdk
jest.mock('firebase-admin', () => ({
  firestore,
  initializeApp: () => initializeAdminApp()
}));

2.我是否使用了正确的版本?

如果使用的是正确的SDK,接下来要检查的是是否使用相同版本的时间戳实现。例如,如果您使用的是Client,那么您应该检查您的package-lock.json是否有唯一的firebase版本。

就我的情况而言,我在不同的时间安装了@firebase/testingfirebase,由于与@firebase/testingfirebase版本依赖性不同,我同时安装了两个不同的firebase包。您可以更新一个旧包来修复这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58774788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档