我有一个微控制器向Google Cloud Pub/Sub服务中的一个主题发送数据,并且我成功地接收到了数据:

我在Firebase中创建了一个Cloud函数,目的是在每次有消息到达名为"sensors“的特定主题时,将接收到的数据存储在数据库中。
这是我的index.js文件:
const functions = require("firebase-functions");
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const app = admin.initializeApp();
const firestore = app.firestore();
var num = 0;
firestore.settings({ timestampsInSnapshots: true });
const auth = app.auth();
exports.deviceState = functions.pubsub.topic('sensors').onPublish(async (message) => {
const deviceId = message.attributes.deviceId;
const deviceRef = firestore.doc(`device-configs/${deviceId}`);
try {
await deviceRef.update({ 'state': message.json, 'online': true, 'timestamp' : admin.firestore.Timestamp.now() });
console.log(`State updated for ${deviceId}`);
} catch (error) {
console.error(`${deviceId} not yet registered to a user`, error);
}
});据我所知,这个云函数应该创建并填充一个数据库,但在我的Firebase项目中没有数据库,以下是来自Firebase的日志:
9:18:51.107 PM
deviceState
Function execution started
9:18:51.148 PM
deviceState
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:179:52)
9:18:51.148 PM
deviceState
aquast1-dev not yet registered to a user Error: 5 NOT_FOUND: No document to update: projects/project-id/databases/(default)/documents/device-configs/aquast1-dev
9:18:51.148 PM
deviceState
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
9:18:51.148 PM
deviceState
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:336:141)
9:18:51.148 PM
deviceState
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181)
9:18:51.148 PM
deviceState
at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:145:78
9:18:51.148 PM
deviceState
at processTicksAndRejections (internal/process/task_queues.js:77:11)
9:18:51.148 PM
deviceState
Caused by: Error
9:18:51.148 PM
deviceState
at WriteBatch.commit (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:417:23)
9:18:51.148 PM
deviceState
at DocumentReference.update (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:393:14)
9:18:51.148 PM
deviceState
at /workspace/index.js:17:23
9:18:51.148 PM
deviceState
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:135:23)
9:18:51.148 PM
deviceState
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
9:18:51.148 PM
deviceState
at runMicrotasks (<anonymous>)
9:18:51.148 PM
deviceState
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
9:18:51.148 PM
deviceState
code: 5,
9:18:51.148 PM
deviceState
details: 'No document to update: projects/project-id/databases/(default)/documents/device-configs/aquast1-dev',
9:18:51.148 PM
deviceState
metadata: Metadata { internalRepr: Map(0) {}, options: {} },
9:18:51.148 PM
deviceState
note: 'Exception occurred in retry method that was not classified as transient'
9:18:51.148 PM
deviceState
}
9:18:51.149 PM
deviceState
Function execution took 43 ms, finished with status: 'ok'
Logs are subject to Cloud Logging's有人能给我指出正确的方向吗?我遗漏了什么?
发布于 2021-10-08 03:04:44
您似乎正在尝试更新一个不存在的文档,请尝试使用"set“而不是" update ",您可以始终将{ merge: true }作为选项传递,然后它将表现为更新
await deviceRef.set({
'state': message.json,
'online': true,
'timestamp' : admin.firestore.Timestamp.now()
}, { merge: true });https://stackoverflow.com/questions/69489858
复制相似问题