Nested array pulled, [ ] was left, what is this in the array?

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.

Assuming JS code.

The attribute length of an array is a number. Comparing it to strings like

and

or with null

is definitively wrong. I am not too savvy in JS, but I suspect it has a length equals to the number 0. In mongosh, it does work this way:

Atlas rent-shard-0 [primary] test> x= []
[]
Atlas rent-shard-0 [primary] test> x.length
0
Atlas rent-shard-0 [primary] test> 

You might also have typo in your code. I one place you write

and at other places you use

lower case m versus upper case M.

Field names are case sensitive:

Atlas rent-shard-0 [primary] test> x = { _id : 1 }
{ _id: 1 }
Atlas rent-shard-0 [primary] test> x._id
1
Atlas rent-shard-0 [primary] test> x._Id

Atlas rent-shard-0 [primary] test>

I’ll test it out and I’ll let you know…

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?

The logic of array is simple.

An empty array named messages ( messages = [ ] ) should mean no messages, rather than an empty message.

An empty message should be an empty object like { }. So if your array has 1 empty message, it should be equal to [ { } ].

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.

Heres the code:

		<% if (messages) { %>
			<% for (let nr = 1; nr <= messages.length; nr++) { %>
				<div class="cardmsgpage">
					<div class="wraptkn">Message <%= [nr] %> : <%= messages[nr-1].msgcontent %>  </div>
				</div>
			<% } %>
		<% } else { %>
			<div class="displayempty">You currently have no messages.</div>
		<% } %>

This else is only applied if there are no messages… working this out now with an if within if(messages)…

I have successfully done it, I was using the if within the if (messages)...

Separated it and used:

<% if (messages == 0) {%>
		<div class="displayempty">You currently have no messages.</div>
<% } %>

Worked like a beauty!

Thank you.

1 Like

//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><%

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.