无法获取云函数以从 Google Cloud IoT Pub/Sub 主题获取数据并将其存储在 Firestore 数据库中

Cannot get a Cloud Function to fetch data from Google Cloud IoT Pub/Sub topic and store it in a Firestore Database

我有一个微控制器向 Google 云 Pub/Sub 服务中的一个主题发送数据,我正在成功接收数据:

我在 Firebase 中创建了一个云函数,目的是在每次消息到达名为“传感器”的特定主题时将接收到的数据存储在数据库中。

这是我的 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);
    }
});

据我了解,此 Cloud Function 应创建并填充数据库,但我的 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

有人能给我指出正确的方向吗?我错过了什么?

您似乎正在尝试更新不存在的文档,请尝试使用“set”而不是“update”,您始终可以将 { merge: true } 作为选项传递,然后它的行为就像更新

await deviceRef.set({ 
    'state': message.json, 
    'online': true, 
    'timestamp' : admin.firestore.Timestamp.now() 
}, { merge: true });