2

I am getting this error when i am exporting modules from file A and importing in file B and when i am running file B it gives that error.that is related to Firebase cloud Firestore.

const mailEvents = (startTime, endTime) => {

  serverRef = db.collection("MailEvents");
  let getDocs = serverRef
    .where("timestamp", ">=", startTime)
    .where("timestamp", "<=", endTime)
    .get()
    .then(querySnapshot => {
      if (querySnapshot) {
        let docs = querySnapshot.docs.map(doc => doc.data());
        console.log(docs)
      }
    });
}
mailEvents();
module.exports.mailEvents = mailEvents;

and the main.js file is

const module = require('./report.js')

module.mailEvents(1575225929,1575305012);
1
  • Your first bit of code is showing a function called "mailEvents", but your second bit of code is calling a function called "mailEventReports". They don't match. Please edit the question with the correct code. Commented Dec 27, 2019 at 19:36

1 Answer 1

3

The problem is that in your module, right before the export, you call the mailEvents() function without any arguments, hence the error "Cannot used "undefined" as a Firestore value" because it's trying to query the collection with startTime and endTime as undefined. In other words, each time you require this file, you're essentially calling the method twice. You can also simplify a few lines here. It should work if you change that file to this:

const mailEvents = (startTime, endTime) => {
    db.collection("MailEvents")
        .where("timestamp", ">=", startTime)
        .where("timestamp", "<=", endTime)
        .get()
        .then(querySnapshot => {
            if (querySnapshot && querySnapshot.length > 0) {
                let docs = querySnapshot.docs.map(doc => doc.data());
                console.log(docs)
            }
        });
};

module.exports = {
    mailEvents
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.