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.