我在尝试播种时在MongoDB中收到错误。终端声明db.collection.remove(...不是一个函数,我不知道出了什么问题。我已经查看了其他堆栈问题以及MongoDB本身在db.collection.remove()上的文档。我确实通过NPM安装了mongo和mongoose ...请参阅下面的代码。
'use strict'
// REQUIRE DB
const db = require('./models');
let tacoList = [
{
name: "Beef Taco Boats",
shell: "Hard",
meat: "Beef",
spicy: false,
toppings: "Rice, Beans, Cheese, Avocado, Guac, Pico, Sour Cream, Shredded Lettuce",
recipe: "http://www.delish.com/cooking/recipe-ideas/recipes/a53019/beef-taco-boats-recipe/",
picture: "http://www.delish.com/cooking/recipe-ideas/recipes/a53019/beef-taco-boats-recipe/?zoomable"
}];
db.Taco.remove({}, (err, taco) => {
console.log('Removing Tacos');
db.Taco.create(tacoList, (err,tacos) => {
if(err){
console.log('DB Creation Error: ', err);
}
console.log('Created Tacos');
process.exit();
});
});
模型文件: tacos.js:
'use strict'
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
const TacoSchema = new Schema ({
name: String,
shell: String,
meat: String,
spicy: Boolean,
toppings: String,
recipe: String,
picture: String
});
// Taco Model
const Taco = mongoose.model('Taco', TacoSchema);
// Export
module.export = Taco;
index.js文件:
'use strict'
// Require Mongoose
const mongoose = require('mongoose');
// DB Connect
mongoose.connect(process.env.MONGODB_URI ||
process.env.MONGOLAB_URI ||
process.env.MONOGOHQ_URL ||
'mongodb://localhost/tacos'
);
// EXPORT TACO
module.exports.Taco = require('./tacos.js');