Node.js 猫鼬赢了';不添加文档
我正在尝试从heroku调度程序将条目添加到mongo db。但目前我只是使用“node-app/bin/trackStats”进行测试 这是我的模型Node.js 猫鼬赢了';不添加文档,node.js,mongodb,mongoose,locomotivejs,Node.js,Mongodb,Mongoose,Locomotivejs,我正在尝试从heroku调度程序将条目添加到mongo db。但目前我只是使用“node-app/bin/trackStats”进行测试 这是我的模型 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var StatsSchema = new Schema({ // eMail address totalblc: { type: Number, required: false}, date: { t
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var StatsSchema = new Schema({
// eMail address
totalblc: { type: Number, required: false},
date: { type: Date, default: Date.now },
difficulty: {type: Number, unique: true},
up: {type: String, required: false},
// Name
});
module.exports = mongoose.model('Stats', StatsSchema);
下面是添加实体的代码
#!/app/bin/node
var request = require('request');
var Stats = require('../models/stats');
function getStats() {
request('http://someurl.com/', function (error, response, body) {
if (!error) {
console.log("tesT");
console.log(body) // Print the google web page.
var obj = JSON.parse(body);
var coins = obj.coins;
var difficulty = obj.difficulty;
var up = obj.status;
var stat = new Stats();
stat.totalblc = coins;
stat.difficulty = difficulty;
stat.up = up;
stat.save(function (err) {
if (err) {
console.log(err);
}
console.log("IN");
});
console.log("aa");
}
else {
console.log(error);
console.log("TEST");
}
});
}
getStats();
It console.正确记录所有数据,但未向数据库添加任何内容。根据您的评论:
我相信我已经找到了错误的根源,那就是数据库地址是在/app/config/initializers/03_mongoose.js中定义的,我的脚本没有读取或注意到这一点。如果我在脚本中手动添加mongoose.connect,它会工作,但由于试图创建两个连接,这会打乱其他页面。所以我还是被卡住了 …你有我学猫鼬时遇到的问题 关于StackOverflow,请参见:
也许你找错地方了。文档将位于
stats
集合中,而不是stats
。我相信我已经找到了错误的根源,那就是数据库地址是在/app/config/initializers/03_mongoose.js中定义的,我的脚本没有读到或注意到这一点。如果我在脚本中手动添加mongoose.connect,它会工作,但由于试图创建两个连接,这会打乱其他页面。因此,我仍然认为,应用程序通常是使用lcm
命令启动的。这不是一个严格的必要条件,您也可以手动实例化机车应用程序,但在执行config/initializers
中的任何内容之前,它仍然是必需的。