Christian Albaladejo
Christian Albaladejo

Reputation: 19

Return users who follow me and I follow

Hi I'm trying to do this function, my problem is that inside the function in the console.log it does return, but in the return it returns as undefined

async function followThisUser(identify_user_id, user_id) {
var following = await Follow.findOne({ "user": identify_user_id, "followed": user_id }).exec((err, follow) => {
    if (err) return handleError(err);
    console.log(follow)
    return follow;
});

var followed = await Follow.findOne({ "user": user_id, "followed": identify_user_id }).exec((err, follow) => {
    if (err) return handleError(err);
    console.log(follow)
    return follow;
});

return {
    following: following,
    followed: followed
}

}

Upvotes: 0

Views: 46

Answers (1)

Deekshith Anand
Deekshith Anand

Reputation: 2682

Well if the console.log is showing correct value, then you can try something like this:

async function followThisUser(identify_user_id, user_id) {
let obj = {}
await Follow.findOne({ "user": identify_user_id, "followed": user_id }).exec((err, follow) => {
    if (err) return handleError(err);
    console.log(follow)
    obj['following'] = follow; //setting the required object value
});

await Follow.findOne({ "user": user_id, "followed": identify_user_id }).exec((err, follow) => {
    if (err) return handleError(err);
    console.log(follow)
    obj['followed'] = follow; //set the required value
});

console.log(obj); // see if the obj has actual value
return obj;
}

As a friendly suggestion try to keep variable names so that they are differentiable from each other. This really helps in the long run!

Hope that helps! Happy coding!

Upvotes: 1

Related Questions