【发布时间】:2018-06-17 14:52:09
【问题描述】:
我正在运行一个 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),
...
});
}
我非常感谢您对此的任何意见,因为很明显这是某种依赖性问题,但是在离线测试时完全相同的版本可以工作。
【问题讨论】:
-
尝试使用
firebase.firestore.GeoPoint而不仅仅是GeoPoint -
使用
admin.firestore.GeoPoint(来自firebase-admin)有效。仍然想知道这是预期用途还是错误,因为不使用最初声明的包中的类似乎有点奇怪。 -
对上述内容的更正:使用
admin.firestore.GeoPoint可以保存 GeoPoint,但之后对该文档的任何操作(如更改侦听器)都会引发异常Error: Argument "latitude" is not a valid number. at Object.exports.(anonymous function) [as isNumber] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:86:15)。只有从文档中删除属性(手动)并在 Web 界面中重新添加新的 GeoPoint 才能解决此问题。 -
我也面临错误:尝试从 Firestore 检索时,参数“纬度”不是有效数字错误。你能解决这个问题吗?
标签: javascript firebase google-cloud-firestore google-cloud-functions