javascript - use for loop in node js? -


i using loop retrieve data mongodb , display on browser problem it iterates first time , stops. first entry output. whereas gives correct output in console log. please help?

var movieschema1 = new mongoose.schema({   name:string,   address:string,   location:string });  var user=mongoose.model('movie', movieschema1, 'movie');  (var = 0; < user.length; i++) {   res.json("iteration " + i);   res.json('name:' + user[i].name + user[i].address); } 

as others have said, build object response, res.json() it. because you're trying send array, use .map() transform each one:

//turn array of users objects want send const responsedata = user.map( (movie, i) => {    return {       iteration: i,       name: movie.name + ' ' + movie.address    }; });  //this converts array string res.json( responsedata ); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -