我正在尝试从 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: { 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();
它 console.logs 正确记录所有数据,但没有任何内容添加到数据库中。