我第一次尝试保存文档。但它不是在储蓄。我没有得到任何错误。我使用cpu.timeStamp
验证了文档对象是否已成功创建,并且还有cpu.save()
函数。但由于某种原因,cpu.save()
根本没有运行,因为没有console.log语句在该函数内打印。此外,当我进入mongo shell时db.cpu.find()
没有返回任何文件。
var mongoose = require( 'mongoose' );
//var models = "./models";
//fs.readdirSync(models).forEach(function(file) {
// require(models+file);
//});
var Cpu = require('./models/cpu.js');
function saveCpu(cpuResult) {
var cpu = new Cpu ({
timeStamp : cpuResult.timestamp,
avaiable : cpuResult.available,
status : cpuResult.status,
metrics : [ { duration : "15m",
data : cpuResult.metrics["15m"].data,
type: cpuResult.metrics["15m"].type,
unit: cpuResult.metrics["15m"].unit
},
{ duration : "5m",
data : cpuResult.metrics["5m"].data,
type: cpuResult.metrics["5m"].type,
unit: cpuResult.metrics["5m"].unit
},
{ duration : "1m",
data : cpuResult.metrics["1m"].data,
type: cpuResult.metrics["1m"].type,
unit: cpuResult.metrics["1m"].unit
}]
});
//FUNCTION DOESN'T SEEM TO RUN AT ALL
cpu.save(function (err, product, numberAffected) {
if (err) { console.log(err) }
console.log(product);
console.log(numberAffected);
});
}