如何从模型向app.js发送Mongodb数据响应

时间:2015-08-13 23:52:04

标签: node.js mongodb

模型/ test.js:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Test');

var LogSchema = new mongoose.Schema({
  logs: String,
  trasaction_id: String,
  stage:String,
  updated_at: { type: Date, default: Date.now }
});

var log = mongoose.model('Log', LogSchema);

exports.find_log = function(req,res){
  log.update(
    { transaction_id:req.id }, 
    { $set: { logs:req.note, stage:req.stage, transaction_id: req.id } }, 
    function(err, result) {
      return result;
    }
  );
}

app.js:

var log = require('./models/test');
var logdetails = log.find_log({id: "55cd20ea304efb0c3be95538",note: "Provisioning has completed, 2) IS_REACHABLE is in progress",stage: 3});

错误:each time logdetails getting **undefined**

我从mongodb获取数据,但无法将该mongodb数据发送到app.js文件中。

1 个答案:

答案 0 :(得分:0)

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Test');

var LogSchema = new mongoose.Schema({
  logs: String,
  trasaction_id: String,
  stage:String,
  updated_at: { type: Date, default: Date.now }
});

var log = mongoose.model('Log', LogSchema);

exports.find_log = function(req, cb){
  log.update(
    { transaction_id:req.id }, 
    { $set: { logs:req.note, stage:req.stage, transaction_id: req.id } }, 
    cb
  );
}

// app.js:

var log = require('./models/test');
var logdetails = log.find_log({
  id: "55cd20ea304efb0c3be95538",
  note: "Provisioning has completed, 2) IS_REACHABLE is in progress",
  stage: 3
}, function(err, res) {
  console.log(res)
});