Rishab Parmar
Rishab Parmar

Reputation: 419

AWS Lambda + Firestore database: add() not inserting document data in collection

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'
}

Upvotes: 0

Views: 533

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

I then tried opening the databaseURL: "https://database_id_here.firebaseio.com" in the browser and it says "not found" as a response in the browser.

This is the expected behavior. Firebase has two databases: Cloud Firestore, and Realtime Database. Your code is using the Firestore API, while the URL is for the Realtime Database, which apparently was never created for this project.

Since you're only trying to use Firestore in your code the fact that Realtime Database was not created is not a problem. In fact, I'd recommend removing that URL from the code, as it's not needed and since it confused you, it may confuse others.

To see your Firestore database, go to htts://firebase.google.com/console, select the project, and then select Firestore in the left navigation.

Upvotes: 1

Related Questions