【问题标题】:Module '@google-cloud/firestore' is not listed as dependency in package.json模块“@google-cloud/firestore”未在 package.json 中列为依赖项
【发布时间】:2019-07-25 01:08:00
【问题描述】:

我正在使用 TypeScript 编写我的 Firebase 云函数。在我的代码中,我需要引用诸如 CollectionReference、QuerySnapshot 等 Firestore 数据类型。例如:

export const Employee: CollectionReference = admin.firestore().collection('Master/Admin/Employees');
or
async function convertToCsv(snapshot: QueryDocumentSnapshot[])

要在我的代码中使用这些类型,我在文件 (.ts) 的顶部使用以下行:

import { QueryDocumentSnapshot, Timestamp, Query, CollectionReference } from '@google-cloud/firestore';

当我在我的 PC 上本地运行 firebase 功能时,此代码可以正常工作,使用:

firebase serve --only 函数

但是当我尝试在我的 firebase 服务器上部署相同的代码时,我收到以下 tslint 错误并且代码部署失败。

Module '@google-cloud/firestore' is not listed as dependency in package.json

如果我在 package.json 中添加依赖项

"dependencies": {
    "body-parser": "^1.18.3",
    ...
    "@google-cloud/firestore": "~1.0.1"
  },

我的函数已部署,但在执行时出现以下错误:

Error: 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:93:27)
    at Query.where (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:916:25)
    at /user_code/lib/services/reports/index.js:53:22

编译失败的java脚本行是(见**):

const firestore_1 = require("@google-cloud/firestore");
let query = collection
                    .where('divisionCode', '==', divisionCode)
                    **.where('zzLastUpdateOn', '>=', firestore_1.Timestamp.fromDate(startDate))**
                    .where('zzLastUpdateOn', '<=', firestore_1.Timestamp.fromDate(endDate))
                    .where('zzIsDeleted', '==', false);

我真的不知道在这里做什么,请帮忙。请知道我对 TypeScript 和 JavaScript 世界很陌生。

请知道,我在时间戳字段中添加值的同一项目中的其他功能工作正常。并且此功能在桌面(本地主机)的云功能模拟模式下也能正常工作。

您的帮助将不胜感激!!!

【问题讨论】:

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


    【解决方案1】:

    我的代码中的以下更改有助于解决问题。

    我现在使用的是“firestore.Timestamp”,而不是使用“Timestamp”。这一变化帮助我进一步发展。

    现在我的代码如下所示:

    TypeScript 代码:

    let query: Query = collection
                    .where(...)
                    .where('zzLastUpdateOn', '>=', firestore.Timestamp.fromDate(startDate))
                    .where('zzLastUpdateOn', '<=', firestore.Timestamp.fromDate(endDate))
                    .where(...);
    

    并生成Java脚本代码:

    let query = collection
                        .where(...)
                        .where('zzLastUpdateOn', '>=', firebase_admin_1.firestore.Timestamp.fromDate(startDate))
                        .where('zzLastUpdateOn', '<=', firebase_admin_1.firestore.Timestamp.fromDate(endDate))
                        .where(...);
    

    我的建议基于https://stackoverflow.com/a/52609487/6943737 查询。

    【讨论】:

    • 这可能不应该是一个答案。它可能应该是对原始问题的编辑,因为问题仍未解决。
    猜你喜欢
    • 2019-10-27
    • 2021-08-16
    • 2020-02-14
    • 1970-01-01
    • 2016-11-05
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多