简体   繁体   English

如何查询猫鼬的返回值

[英]How to query a mongoose returned value

Am trying to query a value that's returned from mongoose callback function, but all I get is TypeError: #<Object> is not a function . 我正在尝试查询从猫鼬回调函数返回的值,但是我得到的只是TypeError: #<Object> is not a function I know it might not be easy to do that, but with the way am querying the db I need to dive more deep from the returned results, without really having to call up the User model again. 我知道这样做可能并不容易,但是通过查询数据库的方式,我需要从返回的结果中更深入地研究,而不必真正调用User模型。 I don't know if there's a way to go around that. 我不知道有没有办法解决这个问题。

 User.findOne({_id: req.user.id}, function(err,user){
        if(err){
            console.log(err);
        } else {
            if(user.plan == 'hit'){
                User.find({plan: 'hit', verified: 'yes'}).lean().exec(function(error,*suc*){
                    if(suc.length < 1){
                        console.log(error);
                        console.log('no user')
                    } else {
                        console.log(suc)

                        **Error throws up right below here**

                        *suc*.find({admintit: 'admin', adminLimit: 200, admincycle: 0}, function(errok,hungad){
                            if(errok){
                                console.log(err)
                            } else {
                                if(hungad.length < 1){
                                    console.log('no hung admin');
                                } else {
                                    console.log(hungad)
                                }
                            }
                        })
                    }
                })
            }
        }
    })

Really trying to query the suc callback result, but all I get is an error, I have tried converting it to an object Object.assign({}, suc) but it still returns same error, that it's not a function. 确实尝试查询suc回调结果,但是我得到的只是一个错误,我尝试将其转换为对象Object.assign({}, suc)但它仍返回相同的错误,这不是函数。

suc is going to be an array of results where the error occurs, meaning if you call find you will get Array.prototype.find and not mongoose find. suc是发生错误的结果数组,这意味着如果调用find ,将得到Array.prototype.find而不是猫鼬的find。

If you actually want to call Array.prototype.find assuming the object looks like: 如果您实际上想调用Array.prototype.find,则假定对象看起来像:

const suc = [{admintit: 'admin', adminLimit: 200, admincycle: 0}, ...]

You should change it to: 您应该将其更改为:

const admin = suc.find(result => result.admintit === 'admin' && result.adminLimit === 200 && result.admincycle === 0)
if (admin) {
    console.log(admin)
} else {
    console.log('no hung admin');
}

If you want to find all you can change find to filter : 如果要查找所有内容,可以将find更改为filter

const admins = suc.filter(result => result.admintit === 'admin' && result.adminLimit === 200 && result.admincycle === 0)
if (admins.length > 0) {
    console.log(admins)
} else {
    console.log('no hung admin');
}

If you were looking to call find for mongoose then you will have to define it on the schema 如果您find为猫鼬打电话,那么您将不得不在模式中对其进行定义

User.method('find', function () {
    // do something
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM