2017-07-25 76 views
0

我想使用一个清单从集合中选择位置并将它们存储到名为ToDelete的数组中:[{_id:String}]。有了这个数组,我想运行一个Collections对象的内部位置并将其删除。如何删除几个嵌套对象使用ToDeleteItems数组

我将集合ID存储为参数,将数组存储为主体。我有控制台记录数组,所以我知道它有我想删除的ID,并正确地将它们传递给服务。

我的服务器端函数应该用猫鼬看起来像什么?这是我最好的尝试。

//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 
     }); 
    }); 
}); 

这里是我的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())); 
    } 

谢谢大家。

+0

这是okie,观察没有问题的功能之上。 – hardy

+0

:( main.bundle.js:576类型错误:error.json不是一个函数 在CatchSubscriber.selector 并返回的200代码是没有的代码的我已经提供 –

+0

是的,但你可以使用响应.json({isSuccess:false,err:err})用于记录错误部分而不会崩溃你的函数 – hardy

回答

0

你可以做这样的事情 -

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}`)); 
    } 
+0

事实证明我需要一个'/'在我的路线之前。 –

相关问题