学习cnode源码遇到的问题,求大大指导
发布于 1个月前 作者 CarlosRen 205 次浏览 来自 问答

菜鸡一只 ,大神轻喷

在app.js里声明 var ctrltest = require('./controller/ctrlDepartment'),执行node app.js报错
carlos[@carlos-virtual-machine](/user/carlos-virtual-machine):~/workspace/nodejs/itsys$ node app.js 
connect to undefined error:  getaddrinfo ENOTFOUND undefined
注释掉var ctrltest = require('./controller/ctrlDepartment')后正常,相关代码贴出来了,请各位大大给看看
app.js
var ctrltest = require('./controller/ctrlDepartment')

/controller/ctrlDepartment.js
var Department    = require('../proxy').Department;
exports.show = function(){
  Department.newAndSave("1","2","3",function(result){
      console.log(result);
  });
}

/proxy/index.js
exports.Department  = require('./department');

/proxy/department.js
var DepartmentModel = require('../model').Deparment;
exports.newAndSave = function (departmentID  , departmentName , departmentPosition, callback) {
  var DepartmentModel  = new DepartmentModel();
  DepartmentModel.DepartmentID   = departmentID;
  DepartmentModel.DepartmentName   = departmentName;
  DepartmentModel.DepartmentPosition   = departmentPosition;
  console.log(DepartmentModel);
  callback(DepartmentModel);
};

/model/index.js
var mongoose = require('mongoose');
var config   = require('../config');

mongoose.connect(config.db, function (err) {
  if (err) {
    console.error('connect to %s error: ', config.db, err.message);
    process.exit(1);
  }
});

// models
require('./Department');
exports.Department  = mongoose.model('DepartmentModel');

/model/Department.js
var mongoose  = require('mongoose');
var Schema    = mongoose.Schema;
var ObjectId  = Schema.ObjectId;

var DepartmentSchema = new Schema({
  DepartmentID         : {type : String,default:"default ID"},
  DepartmentName       : {type : String, default:"default Name"},
  DepartmentPosition   : {type : String, default:"{'p1','p2','p3'}"},
});
mongoose.model("DepartmentModel", DepartmentSchema);
3 回复

看起来像是 config.db 不存在

@alsotang 大大好厉害。。的确是config文件的export写的不对,已改 BTW,能帮忙推荐几个适合的项目学习吗?感觉看书没有看项目有感觉,提升速度慢

回到顶部