使用 Node.JS、Mongoose、MongoDB 和 Express 删除对象时出现问题

Problem in deleting object using Node.JS, Mongoose, MongoDB, and Express

我有一个名为 subcategories 的集合,另一个名为 categories 的集合 集合 categories 有一个名为 subcategroies 的字段。 这是一个数组,其中包含子类别集合中子类别的 ID。

我有一个端点删除/categories/categoryId/subcategoires/subcategoryId

subcategories.delete('/:categoryId/subcategories/:subcategoryId', async (req, res) => {
  try {
    const category = await Category.findById(req.params.categoryId);
    if(!category) return res.status(404).send("Cateory is not found");
    category.subCategories.remove( req.params.subcategoryId );
    const subCategory = await SubCategory.findByIdAndRemove(req.params.subcategoryId);
    if(!subCategory) return res.status(404).send("Subcategory is not found");
    res.status(202).send("Subcategory is deleted successfully");
  } catch (error) {
    res.send(error.message);
  }
}

所以我想从类别模型中的子类别数组中删除已删除子类别的id..

category.subCategories.remove( req.params.subcategoryId );

我尝试了上面找到的这行代码,但它无法使用 MongooseMongoDB快递.

此处,本地转载:

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/test2345", {
    useNewUrlParser: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", async function() {


    await mongoose.connection.db.dropDatabase();
    // we're connected!

    console.log("Connected");

    const subcategoriesSchema = new mongoose.Schema({
        name: String
    });

    const categoriesSchema = new mongoose.Schema({
        name: String,
        subcategories: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Subcategory' //Edit: I'd put the schema. Silly me.
        }]
    });

    const Subcategory = mongoose.model("Subcategory", subcategoriesSchema);
    const Category = mongoose.model("Category", categoriesSchema);

    const cat1 = new Subcategory({
        name: "Pop"
    });
    const cat2 = new Subcategory({
        name: "Rock"
    });

    await cat1.save();
    await cat2.save();

    const ubercat = new Category({
        name: "Music",
        subcategories: [cat1._id, cat2._id]
    });
    await ubercat.save();

    async function removeSubcategory(categoryId, subcategoryId) {
        try {
            const category = await Category.findById(categoryId);
            if (!category) {
                console.log("No such category");
                return null
            }
            category.subcategories.remove(subcategoryId);
            category.save();
            console.log(subcategoryId);
            const subCategory = await Subcategory.findByIdAndRemove(subcategoryId);
            if (!subCategory) {
                console.log("No such subcategory");
                return null
            }
            console.log("Subcategory is deleted successfully");
        } catch (error) {
            console.log(error.message);
        }
    }

    await Subcategory.find(function(err, subcats) {
        if (err) return console.error(err);
        console.log(subcats);
    });

    await Category.find(function(err, cats) {
        if (err) return console.error(err);
        console.log(cats);
    });

    await removeSubcategory(ubercat._id, ubercat.subcategories[0]._id);
    console.log("After modification.");
    await Category.find(function(err, cats) {
        if (err) return console.error(err);
        console.log(cats);
    });

});

您的脚本中的问题是在 category.subCategories.remove( req.params.subcategoryId ); 之后您缺少 category.save();,因此事务未提交到数据库。

添加save后的输出:

Connected
[ { _id: 5f439bf63224885dbc87e5cf, name: 'Pop', __v: 0 },
  { _id: 5f439bf63224885dbc87e5d0, name: 'Rock', __v: 0 } ]
[ { subcategories: [ 5f439bf63224885dbc87e5cf, 5f439bf63224885dbc87e5d0 ],
    _id: 5f439bf63224885dbc87e5d1,
    name: 'Music',
    __v: 0 } ]
5f439bf63224885dbc87e5cf
(node:23996) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
Subcategory is deleted successfully
After modification.
[ { subcategories: [ 5f439bf63224885dbc87e5d0 ],
    _id: 5f439bf63224885dbc87e5d1,
    name: 'Music',
    __v: 1 } ]