exporting firestore in node js

Jefer

im trying to access firestore from an express api, the thing is that i want to define a module with the firestore intialization and export it so i can use it anywhere in my api with the require statement. what ive been trying is to define this

const admin = require("firebase-admin");
var serviceAccount = require("path_to_firebase_generated_key");
var db = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
module.exports.db = db.firestore(); 

but when i try to access to it from another module, and get the collection, it throws me an error:

{"error": "db.collection is not a function"}

this is my code:

var db = require('../firestore/firestore');
     db.collection("collection_name")
          .doc("document_name")
          .set(object_to_insert)
          .then(result => {
             //action to perfom
          });

ive been looking how to make this, but i havent found a solution, anyone knows how can i reach to do this?

Doug Stevenson

In your case, db from the following line;

var db = require('../firestore/firestore');

is going to be an object with a property also called db that you exported from the first file. Perhaps you meant to require it like this:

var firestore = require('../firestore/firestore');
firestore.db.collection(...)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related