I want to get the data from result of mongoose's find method.
SinceIdLog.find({},['since_id','saved_date'],{sort:{'saved_date': -1}, limit:1}, function(err, docs) {
console.log(docs[0]);
// some processes
});
the output of 'console.log' in this case is what I want like following.
{ since_id: '214320642386968576',
saved_date: Sun, 17 Jun 2012 13:16:04 GMT,
_id: 4fddd8941390b38712000143 }
But, when I write like below
SinceIdLog.find({},['since_id','saved_date'],{sort:{'saved_date': -1}, limit:1}, function(err, docs) {
console.log(docs[0].since_id);
// some processes
});
the output is 'undifined'.
Do you know why?
I save the 'since_id' of Twitter API's parameter in MongoDB.
In this code, I want to get the since_id to retrieve the tweets newly updated in using OAuth.
Regards,
Remove the extraneous ]
after since_id
in the console.log
statement.
Maybe you have getter function for since_id
field which doesn't return anything? Check the schema.
I had the same problem once and I think parsing the result helped.
Try the following
SinceIdLog.find({},['since_id','saved_date'],{sort:{'saved_date': -1}, limit:1}, function(err, docs) {
docs = JSON.parse(docs);
console.log(docs[0].since_id);
// some processes
});
Thank you everyone,
I found the cause.
I haven't correctly defined the Schema like following
var Schema = mongoose.Schema,
SinceIdLog = mongoose.model('SinceIdLog', new Schema());
this works when saving the data.
var XXX = new SinceIdLog(Object);
XXX.save(function(err) {});
But it doesn't seem work well in finding.
I modified it and now it's working correctly.
var Schema = mongoose.Schema,
SinceIdLog = new Schema({
since_id: String,
saved_date: Date
}),
SinceIdSchema = mongoose.model('SinceIdLog', SinceIdLog);
Thanks!!