【问题标题】:error after delete item from array从数组中删除项目后出错
【发布时间】:2017-07-18 19:39:23
【问题描述】:

我尝试在单击按钮时从数据库中的数组中删除 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,
}],
});

点是删除选择的项目

【问题讨论】:

    标签: javascript node.js express mongoose


    【解决方案1】:

    当您在 expressjs 路由中使用命名参数时,您可以通过 req.params['yourKey'] 访问该值。在你的情况下,这将是req.params.description

    因此,您可以删除其中一个产品中的描述条目,您可以这样做

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

    【讨论】:

    • 而 req.params.description 等于 undefined
    • 你确定你调用你的api正确吗?因为在您的问题中,您说您调用/api/editProduct/undefined .. 这意味着req.params.description 的值是一个值为'undefined' 的字符串,不等于undefined。尝试使用/api/editProduct/someDescription 调用它
    • 一切似乎更好,但 req.body_id 返回 undefined
    猜你喜欢
    • 1970-01-01
    • 2021-10-19
    • 2021-10-03
    • 1970-01-01
    • 2014-09-21
    • 2015-02-27
    • 2019-06-28
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多