So I'm new to the MEAN stack, and I hit the wall trying to sow MongoDB. I use Mongoose to communicate with the database, and there is a bunch of documentation out there suggesting that I should use samples using populated JSON files.
What I tried:
node-mongo-seed ; Pretty straight forward, but consistently throws errors at the end of the arrays. (Perhaps the missing bson module is faulty?)
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version Seeding files from directory /Users/Antwisted/code/wdi/MEAN/seeds ---------------------- Seeding collection locations err = [SyntaxError: /Users/Antwisted/code/wdi/MEAN/seeds/locations.json: Unexpected token {]
mongoose-seed ; Also pretty straightforward, basically puts JSON objects in a variable before exporting to the database. Promising, but ... more mistakes ...
Successfully initialized mongoose-seed [ 'app/models/locationsModel.js' ] Locations collection cleared Error creating document [0] of Location model Error: Location validation failed Error creating document [1] of Location model Error: Location validation failed Error creating document [2] of Location model Error: Location validation failed...
So, my thoughts were that this was probably a syntax error in the JSON structure, but playing with it did not give any real solutions (or maybe I miss it?). Sample of my JSON:
{ { "header": "Dan Place", "rating": 3, "address": "125 High Street, New York, 10001", "cord1": -73.0812, "cord2": 40.8732, "attributes": ["Hot drinks", "Food", "Premium wifi"], "hours": [ { "days": "Monday - Friday", "hours": "7:00am - 7:00pm", "closed": false }, { "days": "Saturday", "hours": "8:00am - 5:00pm", "closed": false }, { "days": "Sunday", "closed": true } ], "reviews": [ { "rating": 4, "id": ObjectId(), "author": "Philly B.", "timestamp": "new Date('Feb 3, 2016')", "body": "It was fine, but coffee was a bit dull. Nice atmosphere." }, { "rating": 3, "id": ObjectId(), "author": "Tom B.", "timestamp": "new Date('Feb 23, 2016')", "body": "I asked for her number. She said no." } ] }, { "header": "Jared Jive", "rating": 5, "address": "747 Fly Court, New York, 10001", "cord1": -73.0812, "cord2": 40.8732, "attributes": ["Live Music", "Rooftop Bar", "2 Floors"], "hours": [ { "days": "Monday - Friday", "hours": "7:00am - 7:00pm", "closed": false }, { "days": "Saturday", "hours": "8:00am - 5:00pm", "closed": false }, { "days": "Sunday", "closed": true } ], "reviews": [ { "rating": 5, "id": ObjectId(), "author": "Jacob G.", "timestamp": "new Date('Feb 3, 2016')", "body": "Whoa! The music here is wicked good. Definitely going again." }, { "rating": 4, "id": ObjectId(), "author": "Tom B.", "timestamp": "new Date('Feb 23, 2016')", "body": "I asked to play her a tune. She said no." } ] } }
Also, I'm not quite sure how to specify subdocuments in JSON (assuming that I can get the sowing process to work correctly in the first place).
Here is my model:
var mongoose = require('mongoose'); var subHoursSchema = new mongoose.Schema({ days: {type: String, required: true}, opening: String, closing: String, closed: {type: Boolean, required: true} }); var subReviewsSchema = new mongoose.Schema({ rating: {type: Number, required: true, min: 0, max: 5}, author: String, timestamp: {type: Date, "default": Date.now}, body: String }); var locationSchema = new mongoose.Schema({ name: {type: String, required: true}, address: String, rating: {type: Number, "default": 0, min: 0, max: 5}, attributes: [String], coordinates: {type: [Number], index: '2dsphere'}, openHours: [subHoursSchema], reviews: [subReviewsSchema] }); mongoose.model('Location', locationSchema);
Any understanding of how to navigate these issues would be very helpful. Thanks!