javascript - espressjs, node, mongo saving method -
i have function "saveuser", gets value textbox, , updates information of user , uses ajax post object our updateuser service.
function saveuser(event){ event.preventdefault(); var errorcount = 0; $('#edituser input').each(function(index, val) { if($(this).val() === '') { errorcount++; } }); if(errorcount === 0) { var existinguser = { 'username': $('#edituser fieldset input#inputusername').val(), 'email': $('#edituser fieldset input#inputuseremail').val(), 'fullname': $('#edituser fieldset input#inputuserfullname').val(), 'age': $('#edituser fieldset input#inputuserage').val(), 'location': $('#edituser fieldset input#inputuserlocation').val(), 'gender': $('#edituser fieldset input#inputusergender').val() } $.ajax({ type: 'post', data: existinguser, url: '/users/updateuser/' + data_id, datatype: 'json' }).done(function( response ) { if (response.msg === '') { $('#edituser fieldset input').val(''); populatetable(); disablesavebtn(); } else { alert('error: ' + response.msg); } }); } else { alert('please fill in fields'); return false; } };
and code updateuser service.
router.put('/updateuser/:id',function(req, res){ var id = req.params.id; var user = req.body; delete user._id; console.log('updating user: ' + id); console.log(json.stringify(user)); db.collection('userlist', function(err, result) { collection.update({'_id':new bson.objectid(id)}, user, {safe:true}, function(err, result) { if (err) { console.log('error updating user: ' + err); res.send({'error':'an error has occurred'}); } else { console.log('' + result + ' document(s) updated'); res.send(user); } }); }); });
when click users account , edited it, saving it,
i on console:
post /users/updateuser/53aca86c005afcbc0faac5be 404 128ms - 1.08kb
all of operations good, except operation. adding user working good, deletion of user working also, retrieving of user based on id also, except saving operation.
you making post request. router handling put request
use instead
router.post('/updateuser/:id',function(req, res){
Comments
Post a Comment