I am trying to use Firestore on AWS Lambda in Nodejs. Here is the initialization part of the database connection:
var admin = require('firebase-admin');
var serviceAccount = JSON.parse(process.env.cred);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
var db = admin.firestore();
But whenever I try the insertion operation, It does not insert the data in a 'test' collection.
Insertion code:
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);
};
}
In the logs, it says 'Added document with ID: undefined'
EDIT
As highlighted from the answer given by Frank van Puffelen, databaseURL
is not required for initialising application.
Logs from CloudWatch when inserting two documents in a loop:
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"
is the URL corresponding to the Realtime Database, not Firestore. – Renaud Tarnec