1

I'm sure this issue is from my lack of async/await knowledge or general best practices in general. However, I cannot figure out what I am doing wrong. I'm looking to pull a value from my firestore database, include it into an array of "global variables" and then call it when ever I need it in other modules. However when I check the array pulled in the test function, it always returns a promise. The below code is an example of the last thing I tried.

index.js

const vars = require('./variables');

test();
async function test() {
  console.log('Expect database value', await vars.global()['lovePizza']);
}

variables.js (this is working, array updates)

const db = require('./database');

Let globalVars = {
  lovePiza : '',
}
const variables = {
  async global() {
    globalVars['lovePizza'] = (globalVars['lovePizza'] === '') ? db.getLovePizza() : globalVars['lovePizza'];

    return globalVars;
  }
}
module.exports = variables;

database.js (this is working, value gets pulled from db)

const database = {
  async getLovePizza() {
    const usersRef = db.collection('accounts').doc('user1');

    const doc = await usersRef.get();
    if (!doc.exists) {
      console.log('No such document!');
    } else {
      return doc.data()['lovePizza'];
    }
  }
}
module.exports = database;

Terminal Response:

Expect database value undefined

1 Answer 1

1

I saw a problem in your variables.js, I hope the problem is solved with these changes, use await before db.getLovePizza()

use try/catch when you use async/await

const db = require('./database');
Let globalVars = {
  lovePiza : '',
}
async global() {
  if(globalVars['lovePizza'] === ''){
      try {
       let result = await db.getLovePizza()//use await 
       console.log(result);
       globalVars['lovePizza'] = result
      } catch (error) {
        console.log(error)
        //do somthing for error handlig like throw 
      }
  }
  else{
    globalVars['lovePizza'] = globalVars['lovePizza'] //I think there is no need for this
  }
   return globalVars;
}

index.js

  test() {
  let result = await vars.global()
  console.log('Expect database value', result);// added to question
  console.log('Expect database value', result['lovePizza']);// added to question
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for catching that, it helped, but results still come out as undefined. Your answer helps because now in index.js if I define vars.global() as its own variable then call it again in the console.log, it works. However, preferably I could refer to it direct using vars.global()['lovePizza']. Is this not possible?
your welcome, unfortunately I have no experience in google-cloud-firestore, can you added the result of console.log() like part of the added to my answer, may be I can help you, because type of result should check
becuase vars,global() function is a async so first of all you should assign to a variable
ok well noted, so I must assign to a variable. thanks so much

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.