Michael Brenndoerfer
Michael Brenndoerfer

Reputation: 4086

Accessing mongoose query result

Query

 Blog.find({date: 'December 27, 2014'}, function (err, data) {

        console.log("Data: " + data);
        console.log("Value: " + data.coverFilename);
 }

Dump

Data: { _id: 549de8f6afa8b87c2139559d,
  tags: 'tag1, tag2, tag3 hallo',
  permaLink: 'testtitle',
  coverFilename: '3b6f0110-8d53-11e4-9ef3-9503045c44e0.jpg',
  content: 'test',
  date: 'December 27, 2014',
  timestamp: '1419634934689',
  title: 'testtitle',
  __v: 0 }

Value: undefined

coverFilename obviously exists, so why is it undefined if I try to access it? I don't have a clue

Upvotes: 0

Views: 61

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312129

With find, data is an array of matching objects, not just one. Use findOne instead of you only expect a single result.

Blog.findOne({date: 'December 27, 2014'}, function (err, data) {
    console.log("Data: " + data);
    console.log("Value: " + data.coverFilename);
}

Upvotes: 1

Related Questions