我想将参数数据从mongoDB传递到服务器端。 我创建了全局变量diagramData以从diagramModel.find()查询传递到app.post函数。但当 console.log(diagramData) 时它是空数组。有人能帮我弄清楚吗?谢谢
var diagramData = [];
mongoose.connection.on('connected', function () {
diagramModel.find({}, function(err, data) {
diagramData = data;
});
});
app.post('/display_bpmnFile', function(req, res) {
console.log(diagramData);
res.send({
file: diagramData
});
});
最佳答案
<小时/>You will not get value in that since when you call your route then it works as
async nature
so most of the chance your array will be empty.
It's not a proper answer since I am writing your connection in route.
在某个地方写下你的连接并在任何地方使用它
app.post('/display_bpmnFile', function(req, res) {
mongoose.connection.on('connected', function() {//connection code must not be here put it in somewhere else and make it common
diagramModel.find({}, function(err, data) {
if (!err) {
res.send({
file: data
});
}
});
});
});
关于javascript - 如何使用node.js在server.js中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922824/