javascript - 云函数使用来自不同节点模块的 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),
...
});
}
我非常感谢您对此的任何意见,因为很明显这是某种依赖性问题,但是在离线测试时完全相同的版本可以工作。
解决方案
利用gps: new firebase.firestore.GeoPoint(10.0, 10.0)
推荐阅读
- android - Android Studio 中的 HAXM 出现“未知错误”
- r - 如何提高我的 R 代码的处理时间
- ruby-on-rails - Rails dbconsole for PostgreSQL 的自动身份验证
- windows - 如何查看之前从断点执行的线程?
- javascript - 电子文本输入读/写excel表
- java - java堆栈溢出中的递归方法
- javascript - 我想在 JS 中用 DOM 调用数组元素
- python-3.x - 在python中使用带有字符串的下拉菜单过滤数据框
- c# - 将 UIElement 转换为对象?
- python - 在 Ruby 模式下打开 Python 脚本不起作用