node.js - mongoose 查询数据库 回调报错为null?求帮助啊
高洛峰
高洛峰 2017-04-17 16:26:04
0
2
466

//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;

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
迷茫

The problem is vague, the code is messy, and the error message is not uploaded. Don't know what you want to ask. The callback error is null, is it err? Or results?

巴扎黑

This question was a bit urgent when I asked it. Sorry, the question is like this now. If I connect to the database but no collection or document is found, will the callback err display null? My model is exported as module.exports= mongoose.model('account',accountSchema); But the name of the collection built in mongodb is accounts. Is this added by mongose ​​itself?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template