首页 > 解决方案 > 云函数使用来自不同节点模块的 GeoPoint 类

问题描述

我正在运行一个 firebase 云功能和 firebase firestore。当尝试将 GeoPoint 实例存储到 firestore 时,firebase 上会记录以下错误:

Error: Argument "data" is not a valid Document. Detected an object of type "GeoPoint" that doesn't match the expected instance. Please ensure that the Firestore types you are using are from the same NPM package.
at Object.exports.(anonymous function) [as isDocument] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:86:15)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:286:14)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:420:8)
at exports.initializeActivityStructure.functions.firestore.document.onCreate (/user_code/lib/index.js:86:33)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:716:24

该错误表明我使用的 GeoPoint 类不正确。该函数在使用本地运行时运行良好firebase functions:shell

这是我的 package.json

"dependencies": {
  "@google-cloud/firestore": "0.14.1",
  "firebase-admin": "5.12.1",
  "firebase-functions": "1.0.4",
  ... 
}

我在这里尝试了不同的版本,并确定了在离线执行代码时工作的确切版本。

这是引发错误的代码:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as spacetime from 'spacetime';
import { DocumentSnapshot, GeoPoint } from '@google-cloud/firestore';
admin.initializeApp();

...

export const newDocument = functions.firestore.document('/someCollection/{documentKey}').onCreate((snapshot, context) => {
    return snapshot.ref.set({
        gps: new GeoPoint(10.0, 10.0),
       ...
    });
}

我非常感谢您对此的任何意见,因为很明显这是某种依赖性问题,但是在离线测试时完全相同的版本可以工作。

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


利用gps: new firebase.firestore.GeoPoint(10.0, 10.0)


推荐阅读