我知道这听起来像是重复再次问这个问题,我这样做的原因是我已经尝试了这里给出的所有答案,但直到现在我仍然没有解决方案。我不知道这里显示的代码有什么问题。提前谢谢!
var Product = require("../models/product");
var mongoose = require("mongoose");
mongoose.connect("localhost:27017/shopping");
var db = mongoose.connection;
db
.on("error", function (err) {
console.log("Connection Error!!! this's some prompts: ");
console.log(err);
})
.once("open", function () {
console.log("Open DataBase Successfully!!!");
});
var products = [
new Product({
imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png',
title: 'Gothic Video Game',
description: 'Awesome Game!!!!!!',
price: 10
}),
new Product({
imagePath: 'http://cdn.addictinggames.com/newGames/game-links/must-a-mine-dupe-links/must-a-mine-dupe-links.png?c=17',
title: 'Must a Mine',
description: 'blablablabalbla lballdoi dfjflsjoid',
price: 13
}),
new Product({
imagePath: 'http://www.desktopwallpaperhd.net/wallpapers/8/7/halo-reach-wallpaper-background-desktop-games-title-81671.jpg',
title: 'Halo reach, evolved, art, noble',
description: 'Awesome Game!!!!!!',
price: 20
}),
new Product({
imagePath: 'http://www.qqxxzx.com/image-html.php?pic=/images/games-images/games-images-14.jpg',
title: 'Girls on the Street',
description: 'ldode doe odeedd lballdoi dfjflsjoid',
price: 43
}),
new Product({
imagePath: 'http://cdn.addictinggames.com/newGames/game-links/team-drift-cats-links/team-drift-cats-links.png?c=17',
title: 'Team Drifts Card lives',
description: 'magnifull Game!!!!!!',
price: 22
}),
new Product({
imagePath: 'https://lh3.ggpht.com/OoYWDD31U4GypmhoLAHAy7g9QfENitgoTGcFCC55jRS9wNXLk4Viutp5wJLFzOjgcg=h900',
title: 'Car Race by Fun',
description: 'dlfjdl gofioreo godiee joid',
price: 18
})
];
var done= 0;
for (var i = 0; i < products.length; i++) {
products[i].save(function(err, result){
done++;
if(done === products.length)
exit();
});
}
function exit(){
mongoose.disconnect();
}
以下是型号代码:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var schema = new Schema({
imagePath: {type: String, required: true},
title: {type: String, required: true},
description: {type: String, required: true},
price: {type: Number, required: true}
});
module.exports = mongoose.model("Product", schema);