Node / Mongo:db.collection(' name')。find()。toArray不返回结果

时间:2018-03-14 16:49:21

标签: javascript node.js mongodb

db.collection(' name')。find()。toArray不会将结果返回给我的应用程序。当我在console.log结果时,它显示该函数正在运行。但是,它没有将结果返回到我的应用程序。

[recipelib.js]

obj.getOne = function(id){
var recipe = "";
const myDatabase = db.db("recipe_app_db");
var o_id = new mongo.ObjectId(id);
myDatabase.collection("recipes").findOne({'_id': o_id}, function(err, 
result){
   if (err) throw err;
   recipe = result;

   /*console.log is returning the appropriate result*/
   console.log("recipe found:", recipe);

   /*not returning recipe to my app*/
   return recipe;
});
}


[app.js]

app.get('/recipe/:id', function(req,res){
  console.log("get one recipe called");
  var recipe = recipelib.getOne(req.params.id);
  res.json(recipe);
});

0 个答案:

没有答案