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?
javascript express mongoose ecmascript-6 es6-promise
add a comment |
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?
javascript express mongoose ecmascript-6 es6-promise
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
add a comment |
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?
javascript express mongoose ecmascript-6 es6-promise
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
javascript express mongoose ecmascript-6 es6-promise
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
if (!personas.length)
or if (personas.length === 0)
. If personas
is an empty array or object, it is not falsey.
answered Nov 10 at 6:16
Jordan
848
848
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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