These are chat archives for Automattic/mongoose

6th
Jun 2018
panigrah
@panigrah
Jun 06 2018 11:08
@limpid-kzonix if aggregate doesn’t work for you consider using virtuals. But the field name needs to be different. https://futurestud.io/tutorials/understanding-virtuals-in-mongoose
Chris Rutherford
@cjrutherford
Jun 06 2018 13:20
@lineus Thanks for this. The biggest issue is my route path is leaking into my mongoose call. so..... I need to get over to the express side of things and see why it's doing that......
Kev
@lineus
Jun 06 2018 13:21
can you turn on mongoose debug and see if that's actually happening?
mongoose.set('debug', true)
@cjrutherford do you see that error after fixing the projection object?
Chris Rutherford
@cjrutherford
Jun 06 2018 13:23
yes.... that's the thing that most haven't been understanding. It's the "list" from '/list' in the route definition that is in the error.
Kev
@lineus
Jun 06 2018 13:24
what does your find call look like now?
Chris Rutherford
@cjrutherford
Jun 06 2018 13:25
I've tried it with several different options. here's the current.
router.get('/list', (req,res) => {
  Insider.find({},{id:1}).then(insdiers => {
    if(!insiders) return res.status(400).json({error: "unable to locate insiders"});
    res.json(insiders);
  }).catch(err => res.status(400).json(err));
});
Kev
@lineus
Jun 06 2018 13:27
your .then parameter is misspelled insdiers should be insiders
panigrah
@panigrah
Jun 06 2018 13:27
@cjrutherford I am not familiar with Appolo express bindings, but should you not be returning a value from that route? E.g. return res.json()
And in express do you have to return the Insider.find()?
Ah ignore me. Just looked up express - you have it correct
I am coming from hapijs where values are returned from routes
Chris Rutherford
@cjrutherford
Jun 06 2018 13:45
@lineus Another typo.... I've got to get better at spotting these.
okay, now testing again with
router.get('/list', (req, res) => {
  Insider.find({}, {_id: 1}).then(insiders => {
    if (!insiders) {
      return res.status(400).json({ error: 'unable to find list of insiders' });
    }
    res.json(insiders);
  }).catch(err => res.status(400).json(err));
});
panigrah
@panigrah
Jun 06 2018 13:51
@cjrutherford could you try return res.json(insiders)
Reading through express it seems like a return is safer to have because it will make express wait for any promises to be resolved.
Chris Rutherford
@cjrutherford
Jun 06 2018 13:52
in this case, it's an implicit return. Standard procedure for an express route. only return if not on the happy path.... I think
Kev
@lineus
Jun 06 2018 13:52
did you get the error again @cjrutherford ?
panigrah
@panigrah
Jun 06 2018 13:52
Or flip the promise to a callback in the mongooses call
Chris Rutherford
@cjrutherford
Jun 06 2018 13:53
I had to switch machines in between when you sent the potential fix, so it'll be a bit. have to rewrite my .env file
Kev
@lineus
Jun 06 2018 13:53
HURRY UP I'M ON THE EDGE OF MY SEAT!
Chris Rutherford
@cjrutherford
Jun 06 2018 13:56
still the same. I'm going to forget about this route. for now.
it's been the worst for this project so far. Also going to have to take down that medium post that recommends this type of route.
Kev
@lineus
Jun 06 2018 13:58
if the code is public, I'd be happy to clone it and try to debug :)
Chris Rutherford
@cjrutherford
Jun 06 2018 13:59
I'll make you a contributor. This is for work, and I'm a solo dev here. I can't make it public as the non-profit I work for is really paranoid about anonymity.... is that okay?
Kev
@lineus
Jun 06 2018 13:59
yeah, of course :) sounds like fun.
Chris Rutherford
@cjrutherford
Jun 06 2018 14:00
invite sent
Kev
@lineus
Jun 06 2018 14:01
I created a pull request for updating the api docs to reflect that a string will work as the projection field in find*, so if nothing else you helped make the docs better :smile:
Chris Rutherford
@cjrutherford
Jun 06 2018 14:01
image.png
.env variables are like this:
also the logs folder needs to be created before running too..... sorry for all the caveats
panigrah
@panigrah
Jun 06 2018 14:04
@cjrutherford converting from promise to callback also did not work?
Chris Rutherford
@cjrutherford
Jun 06 2018 14:17
I'll rework it to a callback despite it feeling like a callback is a step backwards.
Chris Rutherford
@cjrutherford
Jun 06 2018 14:23
same deal......
thanks though @panigrah
here's the code:
router.get('/list', (req,res) => {
  Insider.find({}, {id: 1}, (err, insiders) => {
    if(err){
      return res.status(400).json(err);
    }
    res.json(insiders);
  });
});
even adding return before the last res.json gives the same thing
Chris Rutherford
@cjrutherford
Jun 06 2018 16:49
it was a route priority issue.
panigrah
@panigrah
Jun 06 2018 17:18
@cjrutherford what was the problem though?
Chris Rutherford
@cjrutherford
Jun 06 2018 17:18
/:insiderId being defined higher in the file caused /list to match it. It was assuming the text 'list' was an objectID.
panigrah
@panigrah
Jun 06 2018 17:19
ok i understand so this route was not even being called
Chris Rutherford
@cjrutherford
Jun 06 2018 17:19
yep