猫鼬虚拟属性和countDocuments问题

时间:2020-06-17 10:44:20

标签: javascript mongoose attributes virtual

我一直坚持在猫鼬中获取虚拟属性,以返回其他模型的文档数量,这是由于我对js的工作方式缺乏了解。我想要的只是能够从虚拟属性中获得不为0的返回值(“ inner n”值),而我获得的属性值始终为0。

categorySchema
    .virtual('num_wordlists')
    .get(function () {
        var n = 0;
        Wordlist.countDocuments({category_id: this._id}, function(err, count) {
            n = count;
            console.log("inner n: " + n);
        });
        console.log("n: " + n);
        return n;
});

当categorySchema的虚拟属性“ num_wordlists”为“ got”时,将生成此跟踪:

n: 0
n: 0
inner n: 26
inner n: 3
inner n: 26
inner n: 3

0 个答案:

没有答案