-1

I wrote this service and when I call it, the JSON response is the group is updated but when I check the group details it is not updated, the old details are still present. I don't know where or what is the issue.

This is my code:

app.post('/api/updateGroup/:group_id', function(req, res) {
    var checkGroup = Group.findOne({'_id': req.params.group_id }).exec();
    checkGroup.addBack(function(err, existingGroup) {
        if (err) {
            res.json({'message': err });
        } else if (existingGroup) {
            var group = existingGroup;
            Group.findOne({'_id': req.params.group_id })
                .execQ()
                .then(function(existingUser) {
                    var friendphoneNumber = req.body.friendphoneNumber.split(',');
                    var friends = [];
                    console.log('existingUser', friendphoneNumber);
                    async.each(friendphoneNumber, function(phonenum, callback) {
                        var phonenum = phonenum.split("\'")[0];
                        console.log('phonenum', phonenum);
                        User.findOne({'phoneNumber': phonenum })
                            .execQ()
                            .then(function(existingFriend) {
                                if (existingFriend === null) {
                                    friends.push({'details': {'phoneNumber': phonenum } });
                                } else {
                                    friends.push({'details': existingFriend });
                                }
                            })
                            .catch(function(err) {
                                console.log('err', err)
                                friends.push({'details': {'phoneNumber': phonenum } });
                            })
                            .done(function() {callback(); });
                    }, function(err) {
                        friends.push({'details': {'phoneNumber': friendphoneNumber } });
                        existingGroup.friends = friends;
                        existingGroup.save();
                        // existingGroup.update({ '_id': req.params.group_id},{ "$set": {'friends': req.body.friendphoneNumber} } , function(err) {
                        // existingGroup.update({'_id': req.params.group_id}, {update[[, 'friends': req.body.friendphoneNumber]}, callback]);
                        existingGroup.update(function(err) {
                            if (err) {
                                res.json({message: err });
                            } else {
                                res.json({success: 1, message: 'Group updated', updatedGroup: existingGroup });
                            }
                        });
                    });
                })
                .catch(function(err) {
                    res.json({success: 0, message: 'user id Not Match. Please try again'});
                }).done(function(events) {});
        } else {
            callback();
        }
    });
});
Community
  • 1
  • 1
AAA
  • 1,957
  • 4
  • 23
  • 42
  • try `existingGroup.save(function(err){if(err)console.error(err)})` and see if there's any error – laggingreflex Jun 01 '15 at 20:42
  • did, no it doesnt enter the err loop, it comes to else, and i can see updated group details as json response.. bt not the same in db :( – AAA Jun 01 '15 at 20:45
  • or may be it just shows that updated for now and it didnt actually update it in db... – AAA Jun 01 '15 at 20:47
  • How are you querying the DB again to see if it updated? – laggingreflex Jun 01 '15 at 20:48
  • check the details of the group with its id in postman – AAA Jun 01 '15 at 20:49
  • {safe: true, upsert: true} what is this line? why is it used? is there any problem in my code because i didnt use it? – AAA Jun 01 '15 at 21:01
  • [`upsert` - if true and no records match the query, insert update as a new record](https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#update-options) If `group.friends` didn't exist already you must use it. – laggingreflex Jun 01 '15 at 21:09
  • i have group.friends – AAA Jun 01 '15 at 21:11

1 Answers1

0

I solved it (with some help though):

async.each(friendphoneNumber, function(phonenum, callback) {

                phonenum = phonenum.split("\'")[0];
                console.log('phonenum', phonenum);

                User.findOne({
                    'phoneNumber': phonenum
                })
                .execQ()
                 .then(function(existingFriend) {

                    if(existingFriend === null) {
                        friends.push({
                            'details': {
                                'phoneNumber': phonenum
                            }
                        });
                    } else {

                        friends.push({'details': existingFriend});
                    }

                })
                .catch(function(err) {
                    console.log('err', err)
                    friends.push({
                        'details': {
                            'phoneNumber': phonenum
                        }
                    });
                 })
                .done (function() {
                    callback();
                });

            }, function(err) {
                friends.push({
                        'details': {
                            'phoneNumber': friendphoneNumber
                        }
                    });
                group.friends = friends;


                Group.update({ '_id': req.params.group_id}, { $set: { 'friends': {'details': req.body.friendphoneNumber } } }, {safe: true, upsert: true}, function (err, user) {   
                    if (err) {
                        res.json({
                            message: err
                        });
                    } else {

                        res.json({
                            success: 1,
                            message: 'Group updated',
                            group: group
                        });
                    }
                });


            });

        })
        .catch(function(err) {
            res.json({
                success: 0,
                message: 'user id Not Match. Please try again'
            });

        })
        .done(function(events) {


        });
    }
    else {
          callback();

    }

});
halfer
  • 19,824
  • 17
  • 99
  • 186
AAA
  • 1,957
  • 4
  • 23
  • 42