-3

I do not know how something so simple does not work in nodejs, and javascript. I don't know how to resolve this. I want to wait to get the list of movies from external and parse it before returning the data. This is my first time using express or needle. But I think its too complex trying to wait for all the data before returning. what is the best wait out, please.

  App.get("/users", async (request, response) => {
  //console.log("movieDB");
  //database call works fine
  let userlist = await moviedb.getUsers();
  //console.log(userlist);


  let userMovieList = await getMovieUserObj(userlist);


  // Promise.resolve(userMovieList);
  console.log(userMovieList[0]);

  //console.log(userlist);
  response.send("see console" + userMovieList);
  response.end();
});

function getMovieUserObj(userlist) {
  return userlist.map(async (user) => {

    let movieInfoObj = getMovieData(user.favourite_movies);
    console.log(movieInfoObj);
    return {
      id: user.id,
      name: user.firstName + " " + user.lastName,
      favourite_movies: movieInfoObj,
    };
  }
  );
}


async function getMovieData(favourite_movies) {
  var favMovies = [];
  var movieArray = favourite_movies.split(",");
  var itemsProcessed = 0;

  return movieArray.map(async function (movieID) {

    param.i = movieID;

    return needle("get", omdbURL, param, { json: false })
      .then(function (res) {
        //return res.body;
        let movieDetail = res.body;
        let tempObj = {
          ID: movieDetail.imdbID,
          Title: movieDetail.Title,
          Year: movieDetail.Year,
          Plot: movieDetail.Plot,
          Poster: movieDetail.Poster,
        };
        console.log("short details of movie" + tempObj.ID);
        //console.log(tempObj);
        return tempObj;
      })
      .catch(function (err) {
        console.log("Error " + err);
        return [];
      });

    return movieOb;
  });
}

output from console.

sqlite does not support inserting default values. Set the useNullAsDefault flag to hide this warning. (see docs

Listening to port :8080   Promise {   [
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> }   ] } Promise {   [
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> }   ] } Promise {   [
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> }   ] } Promise { [ Promise { <pending> }, Promise { <pending> } ] } Promise { [ Promise { <pending> } ] } Promise {   { id: 1, name: 'Anona Cruz', favourite_movies: Promise { [Array] } } } short details of moviett0848228 short details of moviett4154796 short details of moviett0120575 short details of moviett4154756 short details of moviett4154756 short details of moviett4116284 short details of moviett10515848 short details of moviett0103776 short details of moviett2313197 short details of moviett0389860 short details of moviett0287871 short details of moviett0417741 short details of moviett2975590 short details of moviett0926084 short details of moviett2395427

The express function is returning a promise object I want the things to happen in sequence.

  • What are you expecting `await movieArray.map(...)` to do? `.map()` returns an array so you're doing `await` on an array. That doesn't do anything useful. `await` only does something useful when you `await` a promise (not an array of Promises). Perhaps you want `await Promise.all(movieArray.map(...))`. – jfriend00 Mar 22 '21 at 07:56
  • I get a promise object now. this not helpful @jfriend00 Promise { { id: 1, name: 'Anona Cruz', favourite_movies: [ [Promise], [Promise], [Promise], [Promise] ] } } – user1841144 Mar 22 '21 at 09:03
  • @jfriend00 hi have structured the code a bit better, but when I send the request to the needle, I am not able to get the data just promise. – user1841144 Mar 22 '21 at 11:42
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Mar 22 '21 at 13:21
  • @Liam no this is about async wait and promise. in a loop basically, while loop is possible but it;s not the modern approach. – user1841144 Mar 22 '21 at 17:43
  • The "modern" approach would be [`for..await..of`](https://stackoverflow.com/questions/33438158/best-way-to-call-an-async-function-within-map/64771281) – Liam Mar 23 '21 at 08:30
  • Does this answer your question? [Best way to call an async function within map?](https://stackoverflow.com/questions/33438158/best-way-to-call-an-async-function-within-map/) – Liam Mar 23 '21 at 08:31

1 Answers1

-1

I think i resolved it using

return Promise.all(userlist.map(async (users. 

and

return Promise.all(movieArray.map(async function (movieID) {