Hello, I think I not fully understand how to works with the data.
After a mongo find request I got this kind of result :
[
{
"_id": "601151e7962b1d29063781f9",
"email": "[email protected]"
},
{
"_id": "60115244962b1d29063781fb",
"email": "[email protected]"
}
]
I’m trying to write a boolean function to know if the db return exactly 0 element.
I would like to wrote a function resultAreEmpty which shloud return a boolean.
if(item.length == 0){
return true;
} else {
return false;
}
If I use a function item the result is [false, false]
and I only want it to return false in this case.
If i use a function it asks me too return an object with a json property.
What is the best way to consider the result send by mongogb get as one object only ?
Welcome to the community. @mairkur
[
{
"_id": "601151e7962b1d29063781f9",
"email": "[email protected]"
},
{
"_id": "60115244962b1d29063781fb",
"email": "[email protected]"
}
]
Just to be sure, is the output of the Mongo DB node?
I’m trying to write a boolean function to simply return if the db return 0 or more elements.
Then it will always return? as you will always have 0 or more elements. Not sure if I understand this.
If I use a function item the result is [false, false]
and I only want it to return false in this case.
If i use a function it asks me too return an object with a json property.
What is the best way to consider the result send by mongogb get as one object only ?
By default, if the MongoDB node does not return values the next node will not be executed. You have to go to setting → Always Output Data and set it to true.
After that, you might want to change the function item node to a function node add the following
const results = []
if(items.length === 0){
results.push({ json: { result: true } });
} else {
results.push({ json: { result: false }});
}
return results
Hope that helps.
Thanks for your answer !
Yes that’s it
My sentence is bad sorry, I juste want to know if the mongodb return a result or not.
(example : If the db return 0 elements, the user email is not found then I can create the account.)
Well because when no result are found the mongo node return
[{}]
items.length will be equal to 1 when there is 0 or 1 result found.
How to test the result is something else than [{}] ?
Change it to:
const results = []
if(Object.keys(items[0].json).length === 0){
results.push({ json: { returnedResults: false } });
} else {
results.push({ json: { returnedResults: true }});
}
return results