我正在使用Node/ Express /MongoDB构建一个应用程序(这是我第一次使用所有这些工具),它允许我从数据库中提取数据并将其显示在Express页面上。下面是GET请求的样子:
var str = "";
app.route('/view-reports').get(function(req, res) {
var cursor = collections.find({});
cursor.each(function(err, item) {
if (item != null) {
console.log(str);
str = str + "Substance: " + item.substance + "<br>";
}
if (err) {
console.log(err);
}
});
console.log(str);
res.send(str);
str = "";
});
我预计这将返回类似如下的内容:
物质:A
内容:B
物质:C
但是,初始请求根本不返回任何内容。第二个请求将返回上述内容。如果我将res.send(str)
包含在一个If条件中,那么在发出第二个请求之前,它根本不会加载。
https://stackoverflow.com/questions/50689235
复制相似问题