0

I have this app that allows users to review movies, and I'm trying to add a feature that grabs the poster from the movie and displays it next to the review. I tried to do this using

query database and return list of movies => imdb-id to get the IMDB ID => imdb to get the poster url => add that to a variable and compile with pug

However, I can't get it to work as the array always returns empty first before anything else runs. I've tried to use async waterfall, but I can't figure out how to loop through the array that is returned, and pass the ID to get the url

async.waterfall([
    function (callback) {
        console.log('Request Id:', username);
        var query = "SELECT title, rating, review FROM movies WHERE username = \"" + username + "\" ORDER BY title;";
        callback(null, query);
    }, function (query, callback) {
        // Run query on db
        connection.query(query, callback);
    }, function (rows, fields, cb) {
        // Get array of titles
        var rowArray = rows;
        console.log(rowArray);
    }
],
function(err) {
    // error stuff
})
6
  • Why don't you use Promises or async/await instead? Commented Nov 29, 2017 at 0:52
  • this is async, but I'll check out Promises Commented Nov 29, 2017 at 1:06
  • I am aware that this is also async, however I was speaking about the native async / await which is literally syntactic sugar for promises. I am not sure what node verison / es version you are targeting, so I can't tell you if can use it at all. If you can I can recommend you to use it, as it makes the code a lot more readable. Commented Nov 29, 2017 at 1:28
  • Oh okay. I think I'm actually going to do this with AJAX instead, it would make things a lot easier. Thanks anyways! Commented Nov 29, 2017 at 1:30
  • What? You provided node.js as tag which means you are talking about backend code here. You usually never run db queries from the frontend. Anyways I am posting an answer to explain how you could have solved it. That should help for front- and backend programming. Commented Nov 29, 2017 at 1:32

1 Answer 1

1

I recommend you to use promises or the es2017 async/await instead.

Side note: The first block in your async waterfall is synchronous anyways and you don't perform any lengthy operations there. You could merge the connection.query row into it.

With async/await your code could look like this:

async function sendQuery() {
  console.log('Request Id:', username);
  const query = "SELECT title, rating, review FROM movies WHERE username = \"" + username + "\" ORDER BY title;";

  try {
    const rows = await connection.query(query);
    console.log(rowArray);
  } catch(err) {
     console.log(err);
  }
}
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.