Created
June 23, 2012 18:57
-
-
Save sandeep45/2979483 to your computer and use it in GitHub Desktop.
error while accessing object retrieved from mongodb using mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mongoose1.js | |
// Store a json object with a schema less property | |
// then retrieve them | |
var mongoose = require('/Users/sarneja/projects/node/node_modules/mongoose/index.js'); | |
var colors = require("/Users/sarneja/projects/node/node_modules/colors/colors"); | |
console.info("mongoose loaded: " + mongoose); | |
var myConnection = mongoose.connect('mongodb://localhost/test'); | |
var Schema = mongoose.Schema | |
, ObjectId = Schema.ObjectId; | |
var BlogPost = new Schema({ | |
author : ObjectId | |
, title : String | |
, body : String | |
, date : Date | |
, fbObject : {} // allows me to store any object | |
}); | |
var PostModel = mongoose.model('Post', BlogPost); | |
var firstPost = new PostModel(); | |
firstPost.title="This is my First Post"; | |
firstPost.body="qwq"; | |
firstPost.date=Date.now(); | |
firstPost.fbObject={ // this is the random schema less object | |
"id":"1121", | |
"name":"asa", | |
"aert":"a", | |
"pnumber":"12" | |
}; | |
firstPost.markModified('fbObject'); | |
firstPost.save(function (err) { | |
if(err){ | |
console.info("error while saving: ".red + err); | |
throw err; | |
} | |
console.info("saving a post"); | |
}); | |
PostModel.find({}, function (err, docs) { | |
if(err){ | |
console.info("error while finding: ".red + err); | |
throw err; | |
} | |
docs.forEach(function(doc){ | |
console.info("POST: "); | |
console.info(typeof doc.fbObject); | |
for(var i in doc.fbObject){ | |
console.info(i + " - " + doc.fbObject[i]); | |
} | |
// Next Line fails!! it says that doc.fbObect is undefined. WHY??? | |
console.info(doc.fbObject["aert"]); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment