Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js/mongoose-数组中的子文档;t删除/删除_Javascript_Arrays_Node.js_Mongoose_Subdocument - Fatal编程技术网

Javascript Node.js/mongoose-数组中的子文档;t删除/删除

Javascript Node.js/mongoose-数组中的子文档;t删除/删除,javascript,arrays,node.js,mongoose,subdocument,Javascript,Arrays,Node.js,Mongoose,Subdocument,这是我完成在线训练营后第一次参与自己的node.js项目。删除阵列中的子文档时遇到问题。这里有一些代码,我希望我能提供足够的信息让别人帮助我 models: var productSchema = new mongoose.Schema({ name: String, type: String, location: String }); var clientSchema = new mongoose.Schema({ name: String, add

这是我完成在线训练营后第一次参与自己的node.js项目。删除阵列中的子文档时遇到问题。这里有一些代码,我希望我能提供足够的信息让别人帮助我

models:
var productSchema = new mongoose.Schema({
    name: String,
    type: String,
    location: String 
});

var clientSchema = new mongoose.Schema({
    name: String,
    address: String,
    contactinfo: String,
    products:[]    
});
这是我向客户添加产品的帖子路线,效果非常好:

//Add New Product
app.post("/clients/:id/products", middleware.isLoggedIn, function(req, res){
     Client.findById(req.params.id, function(err, client) {
            if(err){
              console.log(err);
              req.flash('error', "We cannot find the Client!!!");
              return res.redirect("/clients/" + req.params.id + "/products/new");
            }
            Product.create(req.body.product, function(err, product){
                  if(err){
                  req.flash('error', "There was an error adding the product to the user, try again");
                  } else{
                       client.products.push(product);
                       client.save();
                       req.flash('success', "You have added a New Product");
                       res.redirect('/clients/' + req.params.id +'/products/new');
                  } 
            });
     });
});
我的删除路径是我的问题孩子。它会删除产品,但我似乎根本无法将其从阵列中删除。我做了一些研究,并尝试了以下方法:

client.products.find({_id:req.params.product_id}).remove()
client.products.id(req.params.product_id).remove()
client.products.pull({_id: req.params.product_id})
client.find({products:{_id: req.params.product_id}}).remove()
using client.save() right after each
我收到错误或它删除了客户端,
但从不从阵列中删除产品。任何帮助都会很好,或者如果有更好的方法,那也会很好。在寻求帮助之前尝试了一周,所以欢迎熟练开发人员的反馈

哦,这是我最后一次删除路由,我想我会禁用它,直到找到修复程序,这样我才能继续我的项目

//Delete a Product
app.delete("/clients/:id/products/:product_id", middleware.isLoggedIn, function(req, res){
Product.findByIdAndRemove(req.params.product_id, function(err){
    if(err){
        console.log(err);
    } else {
        console.log("Should be deleted now!");
        Client.findById(req.params.id, function(err, client) {
            if(err){
                console.log(err);
            }
            console.log(client.products.length);
            client.find({products: {_id: req.params.product_id}}).remove();
            client.save();
            console.log(client.products.length);
            res.redirect("/clients/");
        });
    }
});
}))


我用来查看是否有任何更改的长度,但从未更改过。

首先,您的方法需要一个
objectID
。试着这样做,看看它是否有效:

Product.findByIdAndRemove(ObjectId(req.params.product_id)

没关系,这里的问题似乎是我的代码。我没有在我的客户机模式中为产品数组推送ref,而是直接在数组中推送产品信息。然后在我的应用程序的一个区域中,我访问产品集合中的数据,但在我的应用程序的另一部分中,我通过产品阵列访问客户集合中的相同信息。根据我的培训(我创建的其他课程应用程序),ref似乎没有被删除,只是无法引用集合,因为它已不在集合中


感谢Laurentiu&Federico的关注,并提供了一些有用的信息。很抱歉,这可能会让您头痛

已查看文档,但不会删除。它删除的是内容而不是ID,因此它保留在MongoDB数据库中

 router.delete('/inventario/:_id', function(req, res){
      Propiedades.findByIdAndDelete(req.params._id, function(err,){
         if (err){
             res.redirect('/inventario/');
         } else {
             res.redirect('/inventario/');
       }
    })
 });


谢谢你提供的信息。我添加了它,但在试用时收到以下错误:ReferenceError:ObjectId未定义编辑版本并收到相同的消息:ReferenceError:ObjectId未定义首先需要ObjectId函数var ObjectId=require('mongodb')。ObjectId;或者改为尝试:Product.findByIdAndRemove(ObjectId({“\u id”:req.params.Product\u id})最终使用:var ObjectId=require('mongodb').ObjectId;Product.findByIdAndRemove(ObjectId(req.params.Product\u id)),function(err)--它从products集合/表中删除了产品,但它仍然保留了数组中的信息。