2

Good day, I would like to know the bug behind my code,

so first I have this db.js code:

/** check if may fb_id */
module.exports.checkfbid =
    async(model, query) => {
        return new Promise((resolve, reject) => {
            if (!query) {
                query = {};
            }
            console.log("pumasok sa checkfbid"+ JSON.stringify(query));
            console.log("pumasok sa checkfbid" + query);
            model.find(query)
                .then(res => {
                    console.log("hindi pa napapalitan: "+res);
                    if(null){
                        resolve(null);
                    }else{
                        console.log("untouched: " + res);
                        resolve(res);
                    }

            })
            .catch(err => {
                console.log("rejected si branch" + err);
                reject(err);
                console.log(err);
                throw err;
            });
    });
}

and this controller.js code

const id_result = await usersessionDB.checkfbid(model, {fb_id: id});// store sa id_result
        console.log("id_result have: " + id_result);// log kung may nkukuha na laman
        if (id_result === null) {// kung null si id_result, create ka new user
              ...
            }
        } else {// else kung hindi null si id_result
              ...
        }

EDIT! Sample output of console.logs

sample output

Please disregard the foreign, unreadable comments. As you can see, console.log("pumasok sa checkfbid"+ JSON.stringify(query)); displays my query from the controller, but when it went inside the promise query, I used

console.log("hindi pa napapalitan: "+res);
                    if(null){
                        resolve(null);
                    }else{
                        console.log("untouched: " + res);
                        resolve(res);
                    }

It doesn't display thing!? I've been stuck for almost 3 hrs.

Summing it up, my problem is how can I define if it's null or undefine if the value return by the promise doesn't return even null or undefine? Is it just blank? or is it the other way around, it's returning a value but what kind of value? blank? Thank you.

Quer
  • 405
  • 7
  • 16
  • 2
    Your code looks good to me. `res` is not a string, it's an object, try to log it this way : `console.log("untouched: " , res)` and same thing with id_result : `console.log("id_result have: " , id_result)` – Jeremy Thille Mar 21 '18 at 16:32
  • it now displays, [] (empty square brackets) `hindi pa napapalitan: [] untouched: [] id_result have: []` but thanks Jeremy Thille, I now can able to detect if it's empty I change `if(Object.keys(res).length === 0){ res = null; resolve(res); }else{ console.log("untouched: ", res); resolve(res); }` and my logic now works <3 Thanks man! – Quer Mar 21 '18 at 16:40
  • Ah cool, glad I could help :) – Jeremy Thille Mar 21 '18 at 16:42
  • [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Mar 21 '18 at 16:52

1 Answers1

1

I'm not sure if you intended this, but you're running a conditional statement against null (if(null) {...) which will always produce a falsey response.

Did you mean if(!res) {...?

A better approach is to remove your if statement and use: resolve(res ? res : null)

Joshua Manns
  • 525
  • 4
  • 6
  • Add a comment to ask a question, a question is not an answer. – Paulie Mar 21 '18 at 16:46
  • Yes, that's my other way to put null to my res variable.But it also produces the same output. – Quer Mar 21 '18 at 16:46
  • This should not have been downvoted. The problem I'm stating is that if you conditionally check against null, it will never reach that block. – Joshua Manns Mar 21 '18 at 16:50
  • @Kenyanke if you read the original answer more carefully you'll see that I embedded the solution in the suggestion. The updated answer I offered gives an alternative as well. We should not assum that because there is a question mark in the answer, that there is no answer present. – Joshua Manns Mar 21 '18 at 16:56