javascript - How To Remove Several Nested Objects Using an Array of ToDeleteItems -
i trying use checklist select locations collection , store them array called todelete : [{_id: string}]. array want run against locations inside of collections object , remove them.
i store collection id param , array body. have console logged array know has id's want delete , correctly passes them service.
what should server side function mongoose? best attempt.
//delete collection locations collectionrouter.post('removelocation/:id', function(req, res, next) { collection.update( {_id: req.params.id}, {$pull: {locations: {_id: {$in:req.body}}}}, function (err, result) { if (err) { return res.status(500).json({ title: 'an error occured', error: err }); } res.status(201).json({ message: 'locations removed', obj: result }); }); });
here collection.service
removecollectionlocation(collection: collection, locations: string[]) { let body = locations; const headers = new headers({ 'content-type': 'application/json'}); const token = localstorage.getitem('token') ? '?token=' + localstorage.getitem('token') : ''; return this.http.post('http://localhost:3000/collection/removelocation/' + collection._id + token , body, {headers: headers}) .map((response: response)=> response.json()) .catch((error: response)=> observable.throw(error.json())); }
thank everyone.
you can -
removecollectionlocation(collection: collection, locations: string[]) { let body = locations; const headers = new headers({ 'content-type': 'application/json'}); const token = localstorage.getitem('token') ? '?token=' + localstorage.getitem('token') : ''; return this.http.post('http://localhost:3000/collection/removelocation/' + collection._id + token , body, {headers: headers}) .map((response: response)=> response.json()) .catch(error => observable.of(`bad promise: ${error}`)); }
Comments
Post a Comment