Hi there, I have been using Mongodb for a while now, and love it.
Got something I noticed, and haven’t been able to fix on my end… My schema created an entry in a collection, this collection then has (a) messages: { msgcontent, _id, _from } nested array, while pulling the message for example [0] then I am left with Messages: { "[ ]" }… In my code I have an if(messages){…}else{ display “you have no messages”… with this pull I am left without empty inbox message, what am I to use when referring to this empty array? I have tried a few options, such as if messages.length == "" or if messages.length == null, or if messages.length == "[ ]" display the empty inbox message… But no success…
Can someone point me in the right direction here? I am lost, and that empty inbox message is one of the cherries on top of the cake, won’t be 100% well without it…
Heeeelp x)
EDIT: this is NOT a check box, its a pair of brackets.
btw, that leaves a dilema in my hand now, if [ ] equals to 0 then, the empty messages wont display, cause there is ‘one message’… that one message, is an empty array… If I leave the if with if (messages.length <1) that will hide one message…
I could leave an undeletable message, but that doesn’t make sense according to my logic here… Gotta think of something… any ideas?
I like your logic, yet should be is something I am not sure if it really is… When created the schema the empty inbox message appears, then when added messages, that field becomes an array… Upon deleting the last message, the array is left with Messages: { [ ] } leaving thus a 0 as the empty inbox field, but a 0 being a number leaves the odd event of counting as a message therefore the empty inbox message does not appear… Is there a way to remove completely the array entry?
Example: Messages: { [1]: { msgcontent } }
Such as $pull and the array is then left with Messages: “” ?
I imagine that means I must change the field type, simply doesn’t add up when deleting messages and coding/figuring out that the last deleted message also transforms the field into string…
But I will try now with the if (…) { … }else{ with this logic of yours.
//const messages is an array of unknown size, ranging from a) null, to b) no data to c) array with data.
//check if messages array exists at all (is not null) and if yes, find its length
if (messages && messages.length !== 0) {
// messages found code goes here
}
else {
// NO messages found code goes here
}
This is possible, especially if you are working in JSX:
messages.length === 0 && %><div class="displayempty">You currently have no messages</div><%