新手求问:通过前台ajax由mongoose获取mongodb里数据时遇到的问题,前台不能获取到数据
nodejs模块连接和获取数据: var http=require(‘http’); http.createServer(function(req,res){ console.log('Request received: '); var mongoose=require(‘mongoose’); mongoose.connect(‘mongodb://127.0.0.1:27017/test’);
var db=mongoose.connection;
db.on('error',console.error.bind(console,'连接失败'));
db.once('open',function(res,callback){
var Schema=mongoose.Schema({
title:String,
connect:String,
time : {type:Date, default: Date.now}
});
var Tag=mongoose.model('Tag',Schema);
var blogtext =new Tag({
title:'第一篇',
connect:'第一篇内容'
});
blogtext.save(function(err){
if(err)return console.error(err);
});
Tag.find(function(err,docs){
if(err) return console.error(err);
console.log(docs);
//return docs;
});
}); res.end(); }).listen(8090,‘127.0.0.1’);
//ajax函数 ajax({ method : ‘post’, url : ‘http://127.0.0.1:8090/’, data : {}, success : function (docs) { var html = ‘’; for (var i = 0; i < docs.length; i ++) { alert(docs[i].title); html += ‘
’ + docs[i].time + ‘’ + docs[i].title + ‘
’ + docs[i].content + ‘
求问一下大神,这是哪里出了错误?