These are chat archives for Automattic/mongoose

5th
Mar 2018
Kev
@lineus
Mar 05 2018 10:01 UTC
@hillct I'd be curious what if any effect overriding Schema.methods has on your ability to use the timestamps option on your schema. from looking at the source for schema, it looks like theres a function that gets attached to schema.methods called initializeTimeStamps.
Kev
@lineus
Mar 05 2018 10:22 UTC
looks like the initializeTimeStamps() method is just a tool for you to call, the actual creation when passing in {timestamps: true} to Schema uses a different method called setupTimeStamp(). gist that shows it doesn't change like I thought it would.
Chris Rutherford
@cjrutherford
Mar 05 2018 16:37 UTC

having just a bit of trouble with my delete route, I'm seeing the request come through, but none of the console logs occur.

  delete(req,res) {
    let _id = req.params.computerId;
    console.dir(req.body);
    Computer.findByIdAndRemove(_id, (err, dbres) => {
      if(err){
        log.error(err);
        res.status(500).send(err);
      }else{
        let routeRes = {
          message: 'Computer Deleted Successfully',
          originalResponse: dbres,
          id: dbres._id
        };
        res.status(200).send(routeRes);
      }
    });
  }

The route appears to be setup properly:
app.delete('/api/comptuer/:computerId', controllers.computerController.delete);
but the output from Postman is this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot DELETE /api/computer/5a9811e19cda68eb2e658a46</pre>
    </body>
</html>
image.png
Chris Rutherford
@cjrutherford
Mar 05 2018 16:54 UTC
also.... how would I keep from getting this error? Updating the path '__v' would create a conflict at '__v'
I don't really know what __v is in the document, and I don't want to touch it.
Kev
@lineus
Mar 05 2018 17:32 UTC
hi @cjrutherford, is that top code block that you pasted a function that gets called from within your delete route?
Chris Rutherford
@cjrutherford
Mar 05 2018 17:32 UTC
yes, that's what's called from the delete route.
one thing , the thing about __v is from my update route handler, so it's unrelated....
Kev
@lineus
Mar 05 2018 17:43 UTC
i was curious whether trying to delete an id that doesn't exist would cause an error, but that just returned a null value for the result, first glance of your delete function looks ok.
Chris Rutherford
@cjrutherford
Mar 05 2018 17:57 UTC
yeah..... just confusing that it's the only id in the DB, and it errors out when I try to delete it.
Kev
@lineus
Mar 05 2018 17:57 UTC

@cjrutherford, my express foo is weak, but I'm pretty sure that error isn't coming from

      if(err){
        log.error(err);
        res.status(500).send(err);

that wouldn't have any html markup. plus, that definitely isn't any err you would get back from mongo*

Chris Rutherford
@cjrutherford
Mar 05 2018 17:59 UTC
of course! that's the error handling portion of the callback, but I'm trying to figure out how to keep the error from happening, and I'm not getting any kind of stack trace or reason why it failed other than "I can't do it." from mongoose....
Kev
@lineus
Mar 05 2018 18:02 UTC
did you use the route?
are there other /api routes that do work?
@cjrutherford if you want to check in a copy of the current state in a branch on github ( or create a gist with the express bits ), I'll spend some time pokin at it with a stick.
Kev
@lineus
Mar 05 2018 18:18 UTC
I've only ever used the express generator, so the routers are in separate files added with .use, that clearly isn't a relevant question. hehe :)
Kev
@lineus
Mar 05 2018 18:27 UTC
express has some calls to the debug module that you can enable, have you tried that yet? you can set an env variable DEBUG=* and get more info.
Chris Rutherford
@cjrutherford
Mar 05 2018 20:00 UTC
@lineus sorry, it turned out to be a typo..... in my route index file, it was listed at '/api/comptuer/:computerId' instead of '/api/computer/:computerId'
Kev
@lineus
Mar 05 2018 20:28 UTC
glad you sorted it out @cjrutherford!
Chris Rutherford
@cjrutherford
Mar 05 2018 20:29 UTC
me too! I can be kind of a ditz
Tiago Correia
@tiagocorreiaalmeida
Mar 05 2018 22:40 UTC
hey guys
correct me if im wrong
await Post.updateMany({ "likes": req.user._id }, { $pull: { likes: req.user._id } })
this will update all the posts collection where the likes array contains that id, the update will just pull of the value
?
documents*
Tiago Correia
@tiagocorreiaalmeida
Mar 05 2018 22:54 UTC
nm got it