mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

With express can't send response in a update promis chain

Open beinnova opened this issue 10 years ago • 0 comments

Hi, I have this situation

[...]
 Classified.findOneAndUpdate({_id: postId}, toUpdate).exec()
        .then(function Response(post) {

            if (!post)
                return res.status(404).json({message: "Post not found", id: postId});

            if (toUpdate.hasOwnProperty('content') && undefined !== toUpdate.content.title)
                extractToUpdate.title = toUpdate.content.title;

            if (toUpdate.hasOwnProperty('details')
                && undefined != toUpdate.details.address
                && undefined !== toUpdate.details.address.fullAddress)
                extractToUpdate.fullAddress = toUpdate.details.address.fullAddress;

            if (Object.keys(extractToUpdate).length > 0) {



               return User.findOneAndUpdate({"classifieds._id": postId},
                    {$set: {"classifieds.$": extractToUpdate}}).exec()

            }else{

                return post.save();
            }


        })
        .then(function Resolve(user) {

            return res.send({id: postId});

        }, function Reject(err) {
            console.log(err);
            res.status(500).json(err);
        });

the problem is when the update successful and return a promis. Seems the res.json() it's not called. If change the $set query (ex. "classifiedszz.$": extractToUpdate}) , work well

beinnova avatar Dec 31 '15 16:12 beinnova