unable to convert JSON object to global array
You are calling responde.render("index");
outside of the callback function from mongo.find
, which means you are not waiting for the data to be retrieved to render the "index.ejs" file.
You need to call response.render("index");
inside of the callback function from mongo.find
, like this:
app.get("/", function (request, response) { model.find({},function (err, data1) { if (err) { response.send({ statusCode: 500, message: 'Data did not selected' }) } else { let wholeArray = Object.keys(data1).map(key => data1[key]); app.locals.wholeArray = wholeArray; response.render("index"); }; });});
Note that you can pass locals temporary locals to res.render as its second argument, like this: res.render("index", wholeArray);
Also, why do you need to create an array? You can send the object returned from mongo directly, which will probably be easier to manipulate in the template engine.