0

Okay, so i could use some help. I know my code is incorrect, whats the easiest way to go about handling this. As you can see my return is going to return an empty variable before the query has been executed.

const fetchall = function fetch(sessionid) {
      let data;
      connection.query(
        'SELECT * FROM LOCATIONS WHERE SESSIONID = ?', [sessionid],
        function(err, rows, fields) {
          data = rows;
      });
      return data;
    }; 

I am using express here and the code for the request looks like this.

  app.post('/api/locations/fetchall', function (req, res) {
    let sesionid = sessions.active();
    let results = locations.fetchall(sesionid);
    res.send(results);
  });

1 Answer 1

1

You have to return data inside the query. I am not sure what module you are using to make your database query. I assume that function(err, rows, fields){} is a callback. That means you have to return data in your query() function and not outside.

Or else, check if your framework is not promise-based, ie. you can call something like

connection.query("your query").then(function(data){
   //return data somewhere in here
})
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.