Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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 无法使用$pull(Mongoose)删除用户模型数组中的对象_Javascript_Reactjs_Database_Mongodb_Mongoose - Fatal编程技术网

Javascript 无法使用$pull(Mongoose)删除用户模型数组中的对象

Javascript 无法使用$pull(Mongoose)删除用户模型数组中的对象,javascript,reactjs,database,mongodb,mongoose,Javascript,Reactjs,Database,Mongodb,Mongoose,我已经尝试了各种方法和方法,我正在尝试使用$pull方法 这是我的用户对象 我以前在用户对象中只有对象id,但是现在我正在删除一个完整的对象,我搜索发布的id,然后尝试将其拉出 router.delete('/testing', (req, res, next) => { postModel.findByIdAndRemove(req.query.postid, (error, returnedDocuments) => { if (error) r

我已经尝试了各种方法和方法,我正在尝试使用$pull方法

这是我的用户对象

我以前在用户对象中只有对象id,但是现在我正在删除一个完整的对象,我搜索发布的id,然后尝试将其拉出

    router.delete('/testing', (req, res, next) => {
    postModel.findByIdAndRemove(req.query.postid, (error, returnedDocuments) => {
        if (error) return next(error);

        userModel.findOneAndUpdate(
            // { email: req.query.email, posts: req.query.postid },
            { email: req.query.email },
            { $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } },
            { new: true },
            function(error, user) {
                if (error) {
                    res.json(error);
                }
                console.log(`Post id ===== ${req.query.postid}`);
                console.log(`Email===== ${req.query.email}`);
                console.log(`returning user====${user}`);
                res.json('Successfully updated user');
            }
        );
    });
});
正在从postmodel中删除帖子,但我无法从users postings Make数组中删除该对象


如能提供任何其他解决方案或见解,了解为何会发生这种情况,我们将不胜感激

问题在于MongoDB在值之前比较类型,并且没有应用隐式类型转换,因此没有:

{ $pull: { posts: { number: req.query.postid } } }
您需要运行:

{ $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } }

问题是MongoDB在值之前比较类型,并且没有应用隐式类型转换,因此没有:

{ $pull: { posts: { number: req.query.postid } } }
您需要运行:

{ $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } }

{$pull:{posts:{number:req.query.postid}}
==>在
posts
数组中是否有名为
number
的属性。因为你提供的样品中没有。对不起,这太奇怪了。。。。我再次复制了它。我还尝试了
{$pull:{“posts.$.number”:req.query.postid}},
但没有cookie:(对于posts.$.number,我得到一个错误:
“errmsg”:“位置运算符没有从查询中找到所需的匹配项。”,
可能是因为我需要输入对象或什么?@jcx3x是
req.query.postid
字符串或
ObjectId
{$pull:{posts:{number:req.query.postid}
==>在
posts
数组中是否有一个名为
number
的属性。因为它不在您提供的示例中。很抱歉,这太奇怪了……我再次复制了它。我还尝试了
{$pull:{“posts.$.number”:req.query.postid},
但没有cookie:(对于posts.$.number,我得到一个错误:
“errmsg”:“位置运算符没有从查询中找到所需的匹配项。”,
可能是因为我需要输入对象或什么?@jcx3x是
req.query.postid
字符串或
ObjectId