我正在运行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),
...
});
}
我真的很感激这方面的任何意见,因为很明显它是某种依赖性问题,但完全相同的版本在离线测试时起作用。
答案 0 :(得分:0)
使用gps: new firebase.firestore.GeoPoint(10.0, 10.0)