我正在关注MDN Express教程,但是我遇到了使用node populatedb命令的问题。它应该用集合填充我的mLab数据库,但它不会,我认为这是由于上面提到的参考错误。如果有人可以帮我解决这个参考错误,我们将不胜感激。
这是我的populatedb.js负责人:
console.log('This script populates some test books, authors, genres and bookinstances to your database. Specified database as argument - e.g.: populatedb mongodb://your_username:your_password@your_dabase_url');
// Get arguments passed on command line
var userArgs = process.argv.slice(2);
if (!userArgs[0].startsWith('mongodb://nathankyle625:*******.@ds137729.mlab.com:37729/local_library_express')) {
console.log('ERROR: You need to specify a valid mongodb URL as the first argument');
return
}
var async = require('async')
var Book = require('./models/book')
var Author = require('./models/author')
var Genre = require('./models/genre')
var BookInstance = require('./models/bookinstance')
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
mongoose.connection.on('error', console.error.bind(console, 'MongoDB
connection error:'));
这是我的bookinstance.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BookInstanceSchema = new Schema(
{
book: { type: Schema.ObjectId, ref: 'Book', required: true },
//reference to the associated book
imprint: {type: String, required: true},
status: {type: String, required: true, enum: ['Available',
'Maintenance', 'Loaned', 'Reserved'], default: 'Maintenance'},
due_back: {type: Date, default: Date.now},
}
);
// Virtual for bookinstance's URL
BookInstanceSchema
.virtual('url')
.get(function () {
return '/catalog/bookinstance/' + this._id;
});
//Export model
module.exports = mongoose.model('BookInstance', BookInstanceSchema);
这是我在运行命令时所得到的:
express-locallibrary-tutorial nathanbeier$ node populatedb mongodb://nathankyle625:*******.@ds137729.mlab.com:37729/local_library_express
ReferenceError: BookInstanceSchema is not defined
at Object.<anonymous> (/Users/nathanbeier/Desktop/express-locallibrary-
tutorial/models/genre.js:10:1)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/nathanbeier/Desktop/express-locallibrary-tutorial/populatedb.js:15:13)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
答案 0 :(得分:0)
您的代码看起来不错,但您错过了./models/genre.js
文件而不是./models/bookinstance.js
引发的错误,请尝试从文件BookInstanceSchema
中删除genre.js
行,或者将其重命名为genre.js.save
,然后重试。