2017-07-18 43 views
-1

我嘗試從點擊按鈕的數據庫中的數組中刪除this項目,但我得到錯誤,我看不到我的錯誤。我有錯誤:引發RangeError:最大調用堆棧大小超出,什麼是更重要的我有/api/editProduct/undefined從數組中刪除項目後的錯誤

廠:

userFactory.deleteDescription = function(description) { 
    return $http.delete('/api/editProduct/' + description) 
} 

API

router.delete('/editProduct/:description', function(req, res){ 
    Product.findOneAndUpdate({ _id: req.body_id }, { $pull: { description: this }}, function(err, product){ 
     if(err) throw err; 
     if(!product){ 
      res.json({ success: false, message: 'No user found' }); 
     } else { 
      console.log('ok') 
     } 
    }); 
}); 

模型

var productSchema = new Schema({ 
title: { 
    type: String, 
    require: true, 
}, 
level: { 
    type: String, 
    require:true, 
}, 
description: [{ 
    type: String, 
    require: true, 
}], 
}); 

enter image description here

刪除所選項目

回答

1

當您在expressjs路由中使用命名參數時,可以通過req.params['yourKey']訪問該值。你的情況,這將是req.params.description

所以你到你的產品之一中刪除一個描述項,你可以做

Product.findOneAndUpdate(
    { _id: req.body_id }, 
    { $pull: { description: req.params.description }}, 
    function(err, product){ 
     ... 
    } 
); 
+0

這是行不通的,因爲產品等於空 –

+0

而req.params .description is undefined –

+0

你確定你正確地調用你的api嗎?因爲在你的問題中,你說你調用了'/ api/editProduct/undefined' ..這意味着'req.params.description'的值是一個值爲'undefined'的字符串,不等於'undefined'。 嘗試使用'/ api/editProduct/someDescription'調用它 – japrescott