0

Am trying to query a value that's returned from mongoose callback function, but all I get is 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. 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.

4
  • Are those asterisks *suc*.find intentional, or for emphasis (not in your actual code)? Commented May 12, 2019 at 0:02
  • @JackBashford they are for emphasis, so as to draw attention to the callback am talking about. Commented May 12, 2019 at 0:03
  • On which line is the error? Can you point it out for us with a comment please? Commented May 12, 2019 at 0:05
  • @JackBashford The error throws up, right on the suc.find() function, every other thing is working correctly. normally the suc is in array of objects. Commented May 12, 2019 at 0:10

1 Answer 1

1

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.

If you actually want to call Array.prototype.find assuming the object looks like:

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:

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

User.method('find', function () {
    // do something
});
Sign up to request clarification or add additional context in comments.

6 Comments

That's a good step forward, please can you elaborate more on how I can archive that with the mongoose method(), still a mongodb newbie :)
Depends what you are trying to do. What is the expected result of hungad? I think what you might be looking for is User.find({admintit: 'admin', adminLimit: 200, admincycle: 0}..., instead of *suc*.find
Yep, but I don't really want to be calling up the User model again. I just want to go on with the already returned value, or maybe how I can achieve that from the mongoose method()
I've updated my answer to either use find or filter on the users that have been returned to find admins.
Sure it did, marked as the correct answer, happy </>
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.