0

我是 mongodb 的新手,我正在尝试将 mongodb 查询的结果投影到 html 页面上,其格式与投影在 mongodb shell 上的格式相同。.[这是shell的输出][1]

server.js 的代码是:

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'));

我尝试了一些来自网络的东西,例如:

我得到的结果是:

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

我尝试了一些来自互联网的东西,但没有像:将 toString 包含到结果中,使用聚合函数,stringify 函数返回类型错误但仍然没有结果。

请帮忙。提前致谢。

4

1 回答 1

0

使用 JSON.stringify(result); 它将数组/对象转换为字符串

于 2018-06-12T08:51:58.017 回答