使用moongoose从架构中的嵌套数组中删除项目 [英] Delete item from nested array in Schema using moongoose

查看:31
本文介绍了使用moongoose从架构中的嵌套数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助从嵌套数组中删除 this 项.我尝试使用 $http.delete 但这种方法从数据库中删除了整个 ObjectID,第二个问题是我无法将删除"按钮上的点击与后端代码连接起来.

我的代码:

var product = new Schema({标题:字符串,价格:字符串,描述:[对象ID]});

索引.html

<div ng-class="{ 'has-success':(editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine), 'has-error':(!editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine) || (!descriptionForm.description.$valid && descriptionForm.$submitted) }"><div class="entry input-group" ng-repeat="item in newDescription track by $index"><strong><input ng-disabled="editProduct.disabled" class="form-control" type="text" name="description" ng-model="newDescription[$index]" required></strong><span class="input-group-btn"><a ng-click="editProduct.deleteDescription(item);"class="btn btn-remove btn-danger"><span class="glyphicon glyphicon-remove"></span></a></span>

<br><button ng-disabled="editProduct.disabled" class="btn btn-primary" type="submit">保存</button></表单>

routes.js

router.put('/editProduct', function(req, res){var editProduct = req.body._id;var options = { multi: true };Product.findOneAndUpdate({ _id: editProduct }, { $pull: { description: req.params.description }}, options, function(err, product){如果(错误)抛出错误;如果(!产品){res.json({ 成功: false, 消息: '找不到产品' });} 别的 {产品.更新(功能(错误){如果(错误){控制台日志(错误);} 别的 {res.json({ 成功: true, message: '描述已删除!'})}});};});});

我也尝试了以下方法:

Product.findOne({'_id' : product.id}, function(err, me){for(var i=0; i<=me.description.length; i++){if (String(me.description[i])==String(uid)){me.description.remove(uid);我.save();}}});

我认为,最大的问题是我不知道如何将这个功能连接到按钮上.

解决方案

请在 Mongoose 更新查询之前尝试 console.log(req.params.description) 并检查输出是否确实有效对象 ID.

如果控制台输出未显示有效的 uid,则问题出在 angular 代码中.最有可能在 editProduct.deleteDescription(item) 函数中.通过传递正确的描述 ID 作为参数来检查您是否正在发出 Http 请求.这可能类似于 item.descriptionId 或 item.id.彻底调试.

I need help with removing this item from nested array. I tried used $http.delete but this method deleted whole ObjectID from database, and second problem is that I can't connect the click on the "removing" button with backend code.

My code:

var product = new Schema({
  title: String,
  price: String,
  description: [ObjectID]
});

Index.html

<form name="editProduct.descriptionForm" ng-submit="editProduct.updateDescription(newDescription, editProduct.descriptionForm.description.$valid)" novalidate>
  <div ng-class="{ 'has-success':(editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine), 'has-error':(!editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine) || (!descriptionForm.description.$valid && descriptionForm.$submitted) }">

    <div class="entry input-group" ng-repeat="item in newDescription track by $index">
      <strong><input ng-disabled="editProduct.disabled" class="form-control" type="text" name="description" ng-model="newDescription[$index]" required></strong>
      <span class="input-group-btn">
        <a ng-click="editProduct.deleteDescription(item);" class="btn btn-remove btn-danger">
            <span class="glyphicon glyphicon-remove"></span>
        </a>
      </span>
    </div>
  </div>
  <br>
  <button ng-disabled="editProduct.disabled" class="btn btn-primary" type="submit">Save</button>
</form>

routes.js

router.put('/editProduct', function(req, res){
  var editProduct = req.body._id;
  var options = { multi: true };
  Product.findOneAndUpdate({ _id: editProduct }, { $pull: { description: req.params.description }}, options, function(err, product){
    if(err) throw err;
    if(!product){
      res.json({ success: false, message: 'Product no found' });
    } else {
      product.update(function(err){
        if(err){
          console.log(err);
        } else {
          res.json({ success: true, message: 'Description removed!'})
        }
      });
    };
  });   
});    

I also tried the following approach:

Product.findOne({'_id' : product.id}, function(err, me){
  for(var i=0; i<=me.description.length; i++){
    if (String(me.description[i])==String(uid)){
      me.description.remove(uid);
      me.save();                         
    }
  }    
});

I think, the biggest problem is that I don't how to connect this function to the button.

解决方案

Please try console.log(req.params.description) Before the Mongoose update query and check if the output is indeed a valid ObjectId.

If the console output is not showing the valid uid, then the problem is in the angular code. Most probably in editProduct.deleteDescription(item) function. Check if you are making Http Request by passing the correct Description Id as the parameter. Thats probably something like item.descriptionId or item.id. Debug thoroughly.

这篇关于使用moongoose从架构中的嵌套数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆