1

Goodmorning, I have the oddest thing. I hope someone can help me with this.

I'm fetching data from MongoDB (via Mongoose) Very simple result

   {
      reqts: 1469468008496
   }

I am trying to access the property reqts It's however undefined. But the result above is very clear about it's existence.

What I am doing is the following

   // This gives me the above result, but doing data.reqts gives me nothing. 
   Couple.findOne().sort('-reqts').select('reqts -_id').exec(function(err, item) {
        var data = item
        response.json(data)
    });

This gives me the object I mentioned before. If I do:

    var data = item.reqts

It gives me nothing in return (response is empty).

Hope someone can help me with this. Thanks!


UPDATED: I am now writing out to console too.

    Couple.findOne().sort('-reqts').select('reqts -_id').exec(function(err, data) {
       if (err) { response.status(500).json({error: err}) }
       else {
           console.log(typeof data)
           console.log(data)
           console.log(data.reqts)
           response.json(data)}
    });

This is what it writes to console.

object
{ reqts: 1469468008496 }
undefined

UPDATED:

This seems to explain it: Dot operator not fetching child properties of a Mongoose Document object

Community
  • 1
  • 1
Marco
  • 2,463
  • 3
  • 23
  • 20
  • why you need to sort only one data? we can see you are using `findOne()` . – Arif Khan Jul 26 '16 at 07:36
  • This gives me the record with the highest reqts value. Does it not? Question still remains tho. Why can't I access data.reqts? – Marco Jul 26 '16 at 07:59
  • Perhaps because `item.reqts` isn't a JSON object. Can you try `response.send(data)` ? – F. Kauder Jul 26 '16 at 08:07
  • That gives me the same result. – Marco Jul 26 '16 at 08:17
  • you should check error before sending `if(err) response.status(500).json({error: err}) else response.json(data)` and client side, you should print on console so that you can see what data you are getting – Arif Khan Jul 26 '16 at 08:28
  • If I do typeof data it say's 'object' as expected – Marco Jul 26 '16 at 08:28
  • What does data['reqts'] give you? – alex Jul 26 '16 at 08:36
  • 1
    I'm sure it's something with your other code. I've used your example code in [minimalistic implementation](https://gist.github.com/bearburger/226b6c52168f51cf87fd3e62faf4788b) and it works as expected - prints number. – CrazyCrow Jul 26 '16 at 08:39
  • 1
    That works Roman.R!! It was the schemaless implementation that made it go off. If I only put a schema in, it gives me the expected results. Still wondering why but that gives me something to work on. :) Can you suggest your comment as an answer then I can upvote it. – Marco Jul 26 '16 at 08:53

1 Answers1

3

Well as you already said - you forgot to define scheme. So next code is working

var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/so');

var Couple = mongoose.model('Couple', { reqts: Number });

var couple = new Couple({ reqts: 1469468008496 });
couple.save(function (err) {
    if (err) {
        console.log(err);
    } else {
        Couple.findOne().sort('-reqts').select('reqts -_id').exec(function(err, data) {
            console.log(data.reqts);
        });
    }
});

But I must say there is a way around this problem. You can access field undefinied in model with data._doc so next code would work too:

var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/so');
var treet = require('treet');

var Couple = mongoose.model('Couple', {ts: Number}); // no reqts, we even can use {}

Couple.findOne().sort('-reqts').select('reqts -_id').exec(function(err, data) {
    console.log(data._doc.reqts);
});

I think undefined fields hiding is made to make simpler sending document right to the output without additional selection of required fields.

CrazyCrow
  • 4,125
  • 1
  • 28
  • 39