Heim > Fragen und Antworten > Hauptteil
//dbconf
var mongo = require('mongo'); //引入mysql模块
var mongoose = require('mongoose'); //引入mongoose模块
var shortid = require('shortid');
var db = mongoose.connect('mongodb://127.0.0.1/FBMS')
var Schema = mongoose.Schema;
db.connection.on('error',function(error){
console.log(error);
})
db.connection.on('open',function(){
console.log('open mongoDB');
})
var accountSchema= new Schema({
_id: {
type: String,
unique: true,
'default': shortid.generate
},
username:{type:String ,index:{unique:true}},
password:String,
avataimgurl:{type:String,default:''},
cellphone:Number,
address:String,
device:{stuff:String},
remarks:String
})
module.exports= mongoose.model('account',accountSchema); //连接对象导出
//account
var dbconf = require('./dbconf'); //导入dbconf模块
var data={code:'200',msg:'',username:''};
var checkAccount={
check:function(_req,_res){
dbconf.find({'username':'wangpeng'},function(err,results,fields){
if(err){
console.log("数据库报错:"+err);
console.log(":"+results);
console.log(":"+fields);
}
});
}
}
module.exports = checkAccount;
巴扎黑2017-04-17 16:28:04
这个问题当事提问的时候有些急,不好意思啊 ,这个问题现在是这样 ,如果连接到数据库但是没有找到集合或者文档 回调err会显示null吗?我的模型导出为module.exports= mongoose.model('account',accountSchema); 但是在mongodb中建的集合的名称为accounts 这个是mongose自己加上的吗?