I am trying to set up a mongodb that holds room object. Can anyone see why there is only garbage being added to my database? my rooms object holds 5 rooms, so the correct amount of things are being added, they just aren't being added properly.
This is how I setup my db:
var setupRoomDB = function(){
var roomSchema = mongoose.Schema({
title: String,
description: String,
exitList: [String]
});
Room = mongoose.model("Room", roomSchema);
addRoomsJS();
}
db.once('open', setupRoomDB);
This works great. Now I want to populate my database with things contained in this object:
var rooms = {
bridge: {
title: "Bridge",
description: "You are on the Bridge. There are big comfy chairs and a big screen here.",
roomExits: ['sickbay'],
},
engineering: {
title: "Engineering",
description: "You are in Engineering. There are lots of funny instruments, many smaller screens, and kind of uncomfortable chairs.",
roomExits: ['sickbay'],
},
etc
This is how I am trying to do this:
var addRoomsJS = function (){
for (var room in rooms){
var addRoom = function (err, rooms){
//if the room is already contained
if (rooms.length!=0){
//res.redirect("/?error=room already exists");
return;
}
var newRoom = new Room({
title:room.title,
description : room.description,
roomExits: room.roomExits
});
newRoom.save();
};
Room.find({title:room.title}, addRoom);
}
}
When I view what is being stored in my db this is what I get:
sarah@superawesome:~/comp2406/adventure-ajax-demo$ mongo
MongoDB shell version: 2.4.6
connecting to: test
> show dbs
local 0.078125GB
test (empty)
users 0.203125GB
> use users
switched to db users
> show collections
rooms
system.indexes
> db.rooms.find()
{ "_id" : ObjectId("529cd5686f854f1512000001"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000002"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000003"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000004"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000005"), "exitList" : [ ], "__v" : 0 }