Reputation: 2903
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 }
Upvotes: 0
Views: 876
Reputation: 59773
exitList
. Yet, when you create the room it's called roomExits
.rooms
object. __v
is Mongoose's versionKey
(Reference and how to disable)When you use for(var x in obj)
, x
is the name of the key, but not the value. So, in your example, it would just be bridge
as a string. You'd need to grab the actual object from the rooms
object:
var r=rooms[room];
You could wrap the contents of the loop with a closure and grab the value:
for (var room in rooms) {
(function(room) {
var addRoom = ... /* your code here */
})(rooms[room]);
}
If you don't use a closure, by the time the async find
function returns, the value of room
will not only just be a string, it will be the last value in the associative array (whatever the last property processed by the in
was).
Upvotes: 3
Reputation: 63159
I don't know what you mean with garbage, but:
__v
field is created by Mongoose and used for checks if you try to edit the array from 2 different processes.Upvotes: 0