Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Mongoose无法从node.js中的数组中提取id_Node.js_Mongodb_Mongoose - Fatal编程技术网

Mongoose无法从node.js中的数组中提取id

Mongoose无法从node.js中的数组中提取id,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我的模式是这样的 var PcardSchema = new Schema({ Comments:[{ type: Schema.Types.ObjectId, ref: 'commentSystem' }] }); module.exports=mongoose.model('Pcard',PcardSchema) 此时,我可以通过该中间件删除与该注释相关联的所有答案,但无法从PcardSchema中提取该注释id async.waterf

我的模式是这样的

var PcardSchema = new Schema({

    Comments:[{
        type: Schema.Types.ObjectId,
        ref: 'commentSystem'
    }]

});
module.exports=mongoose.model('Pcard',PcardSchema)

此时,我可以通过该中间件删除与该注释相关联的所有答案,但无法从PcardSchema中提取该注释id

async.waterfall([
                            function (callback) {
                                commentResults.remove();//deleting all asnwer
                                callback(null);
                            },
                            function (callback) {
                                commentSystem.findByIdAndRemove(req.params.QuesId,function (err , comm) {
                                    if(err){
                                       callback(err);
                                    }else{
                                        callback(null); //here i am deleting the comment and getting that comment id 
                                    }
                                })

                            },
                            function (callback) {
                                **Pcard.update({ _id: req.params.PcardId},
                                    {$pull:{'Comments': {'_id' : req.params.QuesID}}},function (err,rowaffected) {
                                        if(err){** //this area of code is not working
                                            callback(err);
                                        }else{
                                            callback(null);
                                        }
                                    }
                                )
                            }
                        ],
是否可以从前置中间件中的注释数组中提取id?
有什么改进代码的建议吗

因为它只是
{“$pull”:{“Comments”:req.params.QuesID}
。对于“引用”设计,它只是一个“ObjectId数组”。您将其与“嵌入式文档数组”混淆,该数组中存在具有
\u id
字段的文档对象。这不是您的模式中定义的。因此,感谢您的帮助。
async.waterfall([
                            function (callback) {
                                commentResults.remove();//deleting all asnwer
                                callback(null);
                            },
                            function (callback) {
                                commentSystem.findByIdAndRemove(req.params.QuesId,function (err , comm) {
                                    if(err){
                                       callback(err);
                                    }else{
                                        callback(null); //here i am deleting the comment and getting that comment id 
                                    }
                                })

                            },
                            function (callback) {
                                **Pcard.update({ _id: req.params.PcardId},
                                    {$pull:{'Comments': {'_id' : req.params.QuesID}}},function (err,rowaffected) {
                                        if(err){** //this area of code is not working
                                            callback(err);
                                        }else{
                                            callback(null);
                                        }
                                    }
                                )
                            }
                        ],