我正在尝试在Heroku实例上建立基本的MongoDB / Mongoose设置,但是当遵循示例小猫代码(位于https://mongoosejs.com/docs/)并尝试创建记录时,我得到了一个非常引人注目的TypeError,并且未创建记录。它似乎与猫鼬模块的加载有关,但我不确定。我该怎么做才能解决此问题?
我在一个免费的Heroku节点上使用Mongoose 5.5.0和NodeJS 10.15.3,我只是遵循他们自己的示例代码,所以我无法想象自己做错了什么。
需要:
// at top of the js file in question
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Kitten = require('./building/helpers/kitten');
建立连接...
mongoose.connect(MONGO_URL, { useNewUrlParser: true });
this.pageManager.db = this.db = mongoose.connection;
this.db.once('open', function () {
console.log("MG: YAY");
});
最后:
saveRecord: function(args)
{
console.log("MG: Record (should be) saved!");
var fluffy = new Kitten({ name: 'fluffy' }); // ERRORS OUT HERE
fluffy.speak(); // "Meow name is fluffy"
console.log("we got past the creation");
console.log("fluffy is", fluffy); //
console.log("we got past the instance print");
fluffy.save(function (err, fluffy) {
if (err) return console.error(err);
fluffy.speak();
});
return;
}
自然地,我假设我应该能够使用示例代码来创建和保存文档,但这每次都失败,并显示以下打印结果。
MG: Record (should be) saved!
TypeError: Cannot read property 'Decimal128' of null
at Object.<anonymous> (/app/node_modules/mongoose/lib/types/decimal128.js:13:44)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/app/node_modules/mongoose/lib/utils.js:7:17)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)