mongoose 取 model 问题
发布于 1个月前 作者 lzjlvlzj 143 次浏览 来自 问答

我有两个文件 BlogDao.js 和 BaseDao.js

  • BlogDao.js 内容

var MongoseClient = require(“./BaseDao.js”); var mc; if(mc === undefined){ mc = new MongoseClient(); }

var BlogDao = function(){

var BlogSchme = require("../entity/Blog.js");
var mongoose = mc.getMongoose();

** var model = mongoose.model("blogs",BlogSchme);** ** var md = mc.getModel("blogs",BlogSchme);** this.add = function(data) { console.log(“entry blogDao add …”); var entity = new model({"title":data.title, "author":"lzj","comments":[{"body":data.content,"date":new Date()}]}); mc.add(entity); console.log(“leave blogDao add …”); }; this.update = function(data){ // mongo.update(data); }; this.delete = function(data){ // mongo.delete(data); }; this.findById = function(id){ md.findById(id,function(err,doc){

    });
    
    var result = null;
    model.findById(id,function(err,doc){

        console.log("11111111111111");
        if(err){
            console.log(err);
        }else{
            result = doc;
        }
        console.log(doc);
        console.log("------------------------");
        console.log(result);
    });
   // mc.findById(model,id);
    console.log("22222222222");
    return result;
}

}

  • BaseDao.js 内容 var MongoseClient = function() { var mongoose = require(‘mongoose’); mongoose.connect(‘mongodb://127.0.0.1:27017/blog’); //确保单例 var unique = this; if(MongoseClient.unique !== undefined){ return MongoseClient.unique; } this.getMongoose = function(){ return mongoose; }; this.getModel = function (collName, collEntity) { if (collName === undefined || collEntity === undefined) { return null; } var model = mongoose.model(collName, collEntity); return model;

    } } 在BlogDao.js文件中发现 变量model 和 md 不一样,根据id查询的时候model 这个可以正常查询,而md不行。请教这是什么原因?

回到顶部