0

I am new to mongodb , and I am trying to project the result of a mongodb query on html page in the same format as it is projected on the mongodb shell. .[this is the output of shell][1]

the code for server.js is:

const express = require('express'); 
const bodyParser = require('body-parser'); 
const path = require('path');
const exphbps = require ('express-handlebars'); 
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/emailserver"; 
const app = express(); 
//view engine setup 
app.engine('handlebars', exphbps()); 
app.set('viewengine','handlebars'); 
//static folder 
app.use('/public', express.static(path.join(__dirname,'public')));
//body parser middleware 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json());
app.post('/send',(req,res)=>{ 
MongoClient.connect(url, function(err, db) { 
if (err) throw err; 
var dbo=db.db("emailserver"); 
var myquery={userid:"student1@mbmmail.com"};
dbo.collection("users").find(myquery,{_id:0,sentto:1,
recievedfrom:1}).toArray(function(err,result){ 
if(err) throw err; 
console.log(result);
res.render('contact1',{msg:result});
// result of query can be displayed as a msg(just to make it simple) }); 

db.close();
}); 
});
app.listen(3002,() => console.log('server started'));

I tried some stuff from web like :

the result I get is :

the result I get is :
<!-- begin snippet: js hide: false console: true babel: false -->
[ { id: .... , userid:student1@mbmmail.com, sentto:[[object],[object]], recievedfrom[[object]] } ]

I tried some stuff from internet but did not work like: including toString to the result, using aggregate function , stringify function returns type error but still nos result.

please help. thanks in advance.

1 Answers1

0

Use JSON.stringify(result); It transforms the array / object in a string

David Vicente
  • 3,091
  • 1
  • 17
  • 27