These are chat archives for Automattic/mongoose

27th
Sep 2018
Irfan Raza
@irfaan008
Sep 27 2018 04:24
I am building a project with microservice architecture where I have multiple databases for each service (around 30) in one server and I frequently get this error
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server
I can put catch to handle it but why its being failed to connect? Server is on ec2 so connectivity and network is fine
Irfan Raza
@irfaan008
Sep 27 2018 04:40
Is it because of max connection? If yes then what is the limit per server and how can we increase it
Eswan
@Eswan
Sep 27 2018 12:20
Hi guys, does anyone know if it's possible to populate on other field and keep the ID reference ?
I have a schema with "userID" and i would like to populate the user into "user" field, so in my client side i don't have to deal with a user field that can be an ID or a populated object
Kev
@lineus
Sep 27 2018 12:25
@Eswan it sounds like you want to use a virtual
Eswan
@Eswan
Sep 27 2018 12:26
i take a look, thanks
Are you sure i can do that with virtual ?
it looks like virtual is always called when you make a request on the collection
i don't want to execute the populate each time i get an item
Kev
@lineus
Sep 27 2018 12:36
@Eswan They don't. They only get populated if you call .populate() on the virtual name. Here's an example gist
Eswan
@Eswan
Sep 27 2018 12:38
wow thanks
it's working
Kev
@lineus
Sep 27 2018 12:43
anytime :)
Eswan
@Eswan
Sep 27 2018 12:43
i can't realize you did that in 10min
Kev
@lineus
Sep 27 2018 12:43
at least half of it came from a code snippet in vscode :smile:
Eswan
@Eswan
Sep 27 2018 12:44
=D
Maurizio Bellemo
@mbellemo_twitter
Sep 27 2018 19:47

I have a bit of confusion in my head regarding methods that can be called by models and those that can be called by objects (if any).

router.patch('/:id/feedbacks/:feedbackId/upvotes/:upvoteId', authenticate(['user']), (req, res) => {
    const projectId = req.params.id;
    const feedbackId = req.params.feedbackId;
    const upvoteId = req.params.upvoteId;
    const body = _.pick(req.body, ['comments', 'priority']);

    if (!ObjectID.isValid(projectId) || !ObjectID.isValid(feedbackId) || !ObjectID.isValid(upvoteId)) {
        return res.status(404).send();
    }

    body._id = upvoteId;

    Feedback.findOne({
        _id: feedbackId,
        company_id: req.user.company_id,
        project_id: projectId,
        "upvotes._id": upvoteId
    }).then((feedback) => {
        if (!feedback) {
            return res.status(404).send();
        }

        // check whether the user is the owner or the admin
        if (!req.user._id.equals(feedback.owner_id)
            && req.user.roles.indexOf('admin') == -1) {
            return res.status(404).send();
        }

        Feedback.updateOne({
            _id: feedbackId,
            "upvotes._id": upvoteId
        },
            { $set: { 'upvotes.$.comments': body.comments, 'upvotes.$.priority': body.priority } },
            { new: true, runValidators: true }).then((feedback) => {
                if (!feedback) {
                    return res.status(404).send();
                }
                res.send({ feedback });
            }, (e) => {
                console.log(e);
                res.status(400).send();
            });
    }).catch((e) => {
        res.status(400).send();
    });
});

I am currently looking for a Feedback, checking some role permission, and - in case - allowing the user to update it with updateOne. The issue is that updateOne does not retrieve the updated issue which I need. Therefore, I thought to use feedback.update (on the object directly), but I probably must have to change the query slightly (i have no idea how though). Any suggestions? On top of it, I would like the update.feedback to retrieve the updated object.