【发布时间】:2020-12-31 11:21:05
【问题描述】:
我正在尝试在 Nodejs 中的 AWS Lambda 上使用 Firestore。下面是数据库连接的初始化部分:
var admin = require('firebase-admin');
var serviceAccount = JSON.parse(process.env.cred);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
var db = admin.firestore();
但每当我尝试插入操作时,它不会将数据插入到“测试”集合中。
插入代码:
async (events, context) => {
try {
var firestoreResult = await db.collection('packagescan12').add({
packageId: scan.packageId,
status: "unsynced",
timestamp: new Date()
});
console.log('Added document with ID: ', firestoreResult.id);
} catch(err) {
console.log("Error in inserting: ", err);
};
}
在日志中显示'Added document with ID: undefined'
编辑
正如 Frank van Puffelen 给出的答案所强调的那样,初始化应用程序不需要databaseURL。
在循环中插入两个文档时来自 CloudWatch 的日志:
Error in inserting: Error: 4 DEADLINE_EXCEEDED: Deadline exceeded
at Object.callErrorFromStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:342:141)
at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:305:181)
at /opt/nodejs/node_modules/@grpc/grpc-js/build/src/call-stream.js:124:78
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Caused by: Error
at WriteBatch.commit (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/write-batch.js:426:23)
at DocumentReference.create (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/reference.js:285:14)
at CollectionReference.add (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/reference.js:2021:28)
at /var/task/index.js:170:80
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async AsyncIterableX.[Symbol.asyncIterator] (/opt/nodejs/node_modules/ix/asynciterable/operators/map.js:18:28)
at async Object.toArray (/opt/nodejs/node_modules/ix/asynciterable/toarray.js:18:22) {
code: 4,
details: 'Deadline exceeded',
metadata: Metadata { internalRepr: Map {}, options: {} },
note: 'Exception occurred in retry method that was not classified as transient'
}
【问题讨论】:
-
请分享您的插入代码。请注意,
databaseURL: "https://database_id_here.firebaseio.com"是与实时数据库对应的 URL,而不是 Firestore。 -
@RenaudTarnec 我已经进行了编辑,还记录了有关 databaseURL 的信息。我还在编辑中添加了一些相同的信息。
-
您没有在 Firestore 控制台中看到新文档吗??
-
不,我没有,正如我之前提到的,它说添加了 id: undefined
-
从您共享的代码中,它应该保存在数据库中。同样,请注意 Firebase 中有两种不同的数据库服务。注意在查看控制台时不要混淆 RTDB 和 Firestore。 您应该分享整个代码,以便我们进一步帮助您。
标签: node.js firebase google-cloud-firestore firebase-admin