我遇到麻烦需要你的帮助~~ thx
我使用mongoose + superAgent + feedparser + eventProxy来获取Rss并保存这些数据
现在我可以完成解析这些数据但是我无法用moogose保存它们
我有3个模块是dao.js,app.js和service.js
我将dao配置为此代码
var mongoose = require("mongoose"),
db,
modelName = "news"; // 设定操作的collections
mongoose.connect("mongodb://localhost:27017/test");
db = mongoose.connection;
db
.on("error", function (err) {
console.log("Connection Error!!! this's some prompts: ");
console.log(err);
})
.once("open", function () {
console.log("Open DataBase Successfully!!!");
});
// 设置
var newsSchema = mongoose.Schema({
"category": String,
"data": [{
"title": String,
"link": String,
"pubDate": String,
"source": String,
"author": String
}]
});
console.log(newsSchema.ObjectId);
newsSchema.pre("save", function (next) {
if( !this.title ) {
this.title = "未知标题";
}
next();
})
var newsModel = mongoose.model(modelName, newsSchema);
module.exports = {
model: newsModel,
schema: newsSchema,
mongoose,
db
}
并将数据保存为以下代码:
saveData(callback) {
var $self = this;
for(var i = 0; i<$self.result.length; i++) {
new model($self.result[i]).save(function (err) {
if(err) {
console.log(err)
} else {
console.log("数据存储成功!")
}
});
}
callback && callback();
db.close();
}
现在数据无法成功保存,同时保存回调函数不运行
你可以给我一些建议吗?