0

I am new to node/js and I was wondering what I should do to fix my DB call. At the moment the call goes to return all data from my Postgres DB but when I try to store it the values are not yet returned. How should I go about structuring my code so I can wait until the values get stored so that when that info is passed to the next variable it's available? If you look below, you can see that the returnedOrders field will have the undefined value since the value from the db is still being fetched before it gets filled.

function insertOrder() {
     dbConnector.connectToDB();
     const returnedOrders =  await dbConnector.retrieveAllOrders();
     console.log("The returned orders" , returnedOrders);
}

function connectToDB() {
    conn.connect().then((success, error) => {
        if(error) {
            console.log(`There was an error connecting because: ${error}`);
        }else {
            console.log(`Successfully connected to the database: ${success}`);
        }
    });
}


function retrieveAllOrders(){
    const query = `SELECT * from food.pizza`;
        conn.query(query).then((results, error) => {
            if(error) {
                console.log(`There was an error due to: ${error}`);
                return;
            }

            console.log("Successfully connected. Running first query");
            console.log(`Results are: ${JSON.stringify(results.rows[0], null , 2)}}`)
            conn.end();
            return results;
        })
        .catch((error) => {
            console.log(`Unable to retrieve query due to: ${error.message}`)
        })

}

This is the output from my terminal. Here you can see the order things happen in

node app.js 
The returned orders undefined
Successfully connected to the database: [object Object]
Successfully connected. Running first query
Results are: { Here will be the results from the database. }
Mohamed Ali
  • 702
  • 8
  • 29
  • callbacks and/or promises – Kevin B May 27 '21 at 22:13
  • `return conn.query(query)` -- `return conn.connect().then(` -- `async function insertOrder()` – Kevin B May 27 '21 at 22:15
  • Hey Kevin I get this `The returned orders undefined` still in the output. – Mohamed Ali May 27 '21 at 22:17
  • i don't understand this part of your code: `dbConnector.retrieveAllOrders` how does `retrieveAllOrders`, defined below, become a property of `dbConnector`? – Kevin B May 27 '21 at 22:19
  • This method will retrieve all the data from the db. Then I wanted to store that value into a variable. Then I would pass that variable into another method that parses that data and saves the necessary info into another service. – Mohamed Ali May 27 '21 at 22:21
  • What i mean is, why is this line: `const returnedOrders = await dbConnector.retrieveAllOrders();` not `const returnedOrders = await retrieveAllOrders();`, i don't see where you are adding `retrieveAllOrders` to `dbConnector` – Kevin B May 27 '21 at 22:25
  • I would expect it to throw an error as written – Kevin B May 27 '21 at 22:26
  • It's part of the same class. So when I import the class itself, that method is part of the class. Which is why I do the `dbConnector.retrieveAllOrders();` – Mohamed Ali May 27 '21 at 22:28
  • Thanks, that's what i was missing. given the changes in my second comment, the order of your logs in the console should have changed, did they? – Kevin B May 27 '21 at 22:30
  • yes the order has changed. But I still get back undefined for the results. – Mohamed Ali May 27 '21 at 22:31
  • Sounds like you’re still missing a return somewhere – Kevin B May 27 '21 at 22:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232978/discussion-between-codemonkey-and-kevin-b). – Mohamed Ali May 27 '21 at 22:37

0 Answers0