Monday, 3 April 2017

Mongodb not updating the data after put request returns successful

In trying to use the objectid in the mongoose schema as a reference to do a put to update a document in a collection. The console logs its as a success but when I refresh the page or look in the mongo shell nothing changes.

Heres the put in the expressjs router:

router.put('/messageupdate/:empId', function (req, res) {
    var values = req.body;
    console.log(values);
    var empId = req.params.empId;
    console.log(empId);
    Message.update({empId: empId}, values, function(err, values) {
        if (!err) {
            res.json("okay");
        } else {
            res.write("fail");
        }
    });
})

Heres the service method:

updateServiceWithId(message: Message): Observable<any> {
 console.log(message);
    const body = JSON.stringify(message);
    console.log(body);
    const headers = new Headers({'Content-Type': 'application/json'});
    return this.http.put('http://localhost:3000/messageupdate/:empId', body, {headers: headers});

}

Heres the client side method that triggers the put:

onUpdateMessage() {
     var retVal = confirm("Do you want to continue ?");
           if( retVal == true ){
    const message = new Message(this.fname,this.lname,this.empId,this.number,this.occu);
    console.log(this.fname);console.log(this.lname);
    console.log(this.empId);console.log(this.occu);
    this.messages.push(message);
    this.messageService.updateServiceWithId(message)
        .subscribe(
            () => console.log('Success!'),
            error => console.error(error)
        );
    }
    else{
              alert("Edit cancled!");
              return false;
        }
}



via user7629970

No comments:

Post a Comment