How can I write conditional logic with the returned value from mongoose db.collection.find()?









up vote
0
down vote

favorite












Good evening Stack Overflow,



I am working on a personal project and I am trying to figure out why I am not able to get the correct response from my backend API. Below you will find code from one of the routes that I am writing. Here I am trying to get a Persona from the database by its arcana field.



My thought process is that if the mongoose method find() fails to find personas with the given arcana then I should return a 404 error and send back a json response detailing what went wrong.



I thought I would be able to do this by saying if(!personas) where personas is what is returned by the mongoose db.collection.find(). This is not working and my API returns empty square brackets with a 200 OK status. All of my testing is being done through Postman.



The console.logs in the code are my debug statements.



console.log(personas === ? true : false); Returns false



console.log(typeof personas); Returns object



console.log(personas === ? true : false); Returns false



console.log(personas === undefined ? true : false); Returns false



router.get("/personas/by/:arcana", (req, res) => 
const errors = ;
// The below logic is not working. We're not sending a 404 on failure.
Persona.find( arcana: req.params.arcana )
.then(personas =>
console.log(personas === ? true : false);
console.log("What is the type of the personas variable?",typeof personas);
console.log(personas === ? true : false);
if (!personas)
errors.no_personas =
"There are no personas in the compendium with that arcana.";
return res.status(404).json(errors);

res.json(personas);
)
.catch(err => res.status(404).json( personas: "There are no personas." ));
);


How can I write my logic so that the correct status code is returned from the backend?










share|improve this question





















  • You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
    – Jordan
    Nov 10 at 6:12














up vote
0
down vote

favorite












Good evening Stack Overflow,



I am working on a personal project and I am trying to figure out why I am not able to get the correct response from my backend API. Below you will find code from one of the routes that I am writing. Here I am trying to get a Persona from the database by its arcana field.



My thought process is that if the mongoose method find() fails to find personas with the given arcana then I should return a 404 error and send back a json response detailing what went wrong.



I thought I would be able to do this by saying if(!personas) where personas is what is returned by the mongoose db.collection.find(). This is not working and my API returns empty square brackets with a 200 OK status. All of my testing is being done through Postman.



The console.logs in the code are my debug statements.



console.log(personas === ? true : false); Returns false



console.log(typeof personas); Returns object



console.log(personas === ? true : false); Returns false



console.log(personas === undefined ? true : false); Returns false



router.get("/personas/by/:arcana", (req, res) => 
const errors = ;
// The below logic is not working. We're not sending a 404 on failure.
Persona.find( arcana: req.params.arcana )
.then(personas =>
console.log(personas === ? true : false);
console.log("What is the type of the personas variable?",typeof personas);
console.log(personas === ? true : false);
if (!personas)
errors.no_personas =
"There are no personas in the compendium with that arcana.";
return res.status(404).json(errors);

res.json(personas);
)
.catch(err => res.status(404).json( personas: "There are no personas." ));
);


How can I write my logic so that the correct status code is returned from the backend?










share|improve this question





















  • You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
    – Jordan
    Nov 10 at 6:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Good evening Stack Overflow,



I am working on a personal project and I am trying to figure out why I am not able to get the correct response from my backend API. Below you will find code from one of the routes that I am writing. Here I am trying to get a Persona from the database by its arcana field.



My thought process is that if the mongoose method find() fails to find personas with the given arcana then I should return a 404 error and send back a json response detailing what went wrong.



I thought I would be able to do this by saying if(!personas) where personas is what is returned by the mongoose db.collection.find(). This is not working and my API returns empty square brackets with a 200 OK status. All of my testing is being done through Postman.



The console.logs in the code are my debug statements.



console.log(personas === ? true : false); Returns false



console.log(typeof personas); Returns object



console.log(personas === ? true : false); Returns false



console.log(personas === undefined ? true : false); Returns false



router.get("/personas/by/:arcana", (req, res) => 
const errors = ;
// The below logic is not working. We're not sending a 404 on failure.
Persona.find( arcana: req.params.arcana )
.then(personas =>
console.log(personas === ? true : false);
console.log("What is the type of the personas variable?",typeof personas);
console.log(personas === ? true : false);
if (!personas)
errors.no_personas =
"There are no personas in the compendium with that arcana.";
return res.status(404).json(errors);

res.json(personas);
)
.catch(err => res.status(404).json( personas: "There are no personas." ));
);


How can I write my logic so that the correct status code is returned from the backend?










share|improve this question













Good evening Stack Overflow,



I am working on a personal project and I am trying to figure out why I am not able to get the correct response from my backend API. Below you will find code from one of the routes that I am writing. Here I am trying to get a Persona from the database by its arcana field.



My thought process is that if the mongoose method find() fails to find personas with the given arcana then I should return a 404 error and send back a json response detailing what went wrong.



I thought I would be able to do this by saying if(!personas) where personas is what is returned by the mongoose db.collection.find(). This is not working and my API returns empty square brackets with a 200 OK status. All of my testing is being done through Postman.



The console.logs in the code are my debug statements.



console.log(personas === ? true : false); Returns false



console.log(typeof personas); Returns object



console.log(personas === ? true : false); Returns false



console.log(personas === undefined ? true : false); Returns false



router.get("/personas/by/:arcana", (req, res) => 
const errors = ;
// The below logic is not working. We're not sending a 404 on failure.
Persona.find( arcana: req.params.arcana )
.then(personas =>
console.log(personas === ? true : false);
console.log("What is the type of the personas variable?",typeof personas);
console.log(personas === ? true : false);
if (!personas)
errors.no_personas =
"There are no personas in the compendium with that arcana.";
return res.status(404).json(errors);

res.json(personas);
)
.catch(err => res.status(404).json( personas: "There are no personas." ));
);


How can I write my logic so that the correct status code is returned from the backend?







javascript express mongoose ecmascript-6 es6-promise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 3:46









BradleyGamiMarques

709




709











  • You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
    – Jordan
    Nov 10 at 6:12
















  • You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
    – Jordan
    Nov 10 at 6:12















You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
– Jordan
Nov 10 at 6:12




You can't equate 2 objects or arrays using '==='. You would only ever get true if you compared 2 variables that pointed to the same object: const obj1 = test: '1'; const obj2 = obj1; obj1 === obj2 returns true;
– Jordan
Nov 10 at 6:12












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










if (!personas.length) or if (personas.length === 0). If personas is an empty array or object, it is not falsey.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235842%2fhow-can-i-write-conditional-logic-with-the-returned-value-from-mongoose-db-colle%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    if (!personas.length) or if (personas.length === 0). If personas is an empty array or object, it is not falsey.






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      if (!personas.length) or if (personas.length === 0). If personas is an empty array or object, it is not falsey.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        if (!personas.length) or if (personas.length === 0). If personas is an empty array or object, it is not falsey.






        share|improve this answer












        if (!personas.length) or if (personas.length === 0). If personas is an empty array or object, it is not falsey.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 6:16









        Jordan

        848




        848



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235842%2fhow-can-i-write-conditional-logic-with-the-returned-value-from-mongoose-db-colle%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20