0

I really doubt my mind right now and am pretty confused.

I'm building a little chat application, just to expriment with some stuff...

For that, I have a MySQL Database with a table just storing the messages with an ID, the content of the message, the userid and a timestamp.

On the page to display the chat, I then do an ajax request to my backend, which returns the data from the database like so:

$messages = $db->exec('SELECT * FROM message ORDER BY ID DESC LIMIT 50');
echo json_encode($messages);

So what I'm getting from there the last 50 messages, encoded in JSON.

On the page, I then receive that and parse them:

messagesParsed = JSON.parse(messages);

So now, what I've got in that variable is this (shortened a bit)

[
    {
        "ID": "68",
        "content": "oi",
        "uid": "4",
        "time": "2021-07-16 13:29:42"
    },
    {
        "ID": "67",
        "content": "io",
        "uid": "4",
        "time": "2021-07-16 13:29:42"
    },
    {
        "ID": "66",
        "content": "oibioboib",
        "uid": "4",
        "time": "2021-07-16 13:29:42"
    },
    {
        "ID": "65",
        "content": "iob",
        "uid": "4",
        "time": "2021-07-16 13:29:41"
    },
    {
        "ID": "64",
        "content": "b",
        "uid": "4",
        "time": "2021-07-16 13:29:41"
    },
    {
        "ID": "63",
        "content": "boi",
        "uid": "4",
        "time": "2021-07-16 13:29:41"
    },
    {
        "ID": "62",
        "content": "oibio",
        "uid": "4",
        "time": "2021-07-16 13:29:41"
    }
]

Then, I'm looping through this array in reverse, with the goal to display the latest message at the very bottom of the page:

for(var i = messagesParsed.length; i>=0; i--){
                    message = messagesParsed[i];
                    console.log(message);
                  }

So far so good, I now got this as a result (stored in the message variable)

{
    "ID": "32",
    "content": "fn",
    "uid": "4",
    "time": "2021-07-16 13:29:24"
}

Here comes the strange part: I want to only display the content of the chat message (at first) and have tried like 20 different variations of getting the content, so within the for loop, I did something like:

console.log(message.content); console.log(message["content"]); console.log(message[content]);

And some other crazy stuff, but in the end, I'm not getting the console to output the value. Only thing I'm getting there is Cannot read property 'content' of undefined

What am I doing wrong? It's not like the first time I try to access something in a JS object, but it seems like I'm just completely lost on this one.

2
  • 1
    You're starting the reverse loop outside the bounds of the array. Use var i = messagesParsed.length - 1. Commented Jul 16, 2021 at 12:29
  • 2
    @RoryMcCrossan oh cmon... I already though I was completely stupid, didn't think about that actually... Thank you! Commented Jul 16, 2021 at 12:30

3 Answers 3

2

I think the issue is you are trying to start at an element that is out of range (messagesParsed.length). Try using the below - it worked for me on jsbin with your test data provided

for(var i = messagesParsed.length - 1 ; i>=0; i--){
                    message = messagesParsed[i]
                    console.log(message.content)
                  }
Sign up to request clarification or add additional context in comments.

Comments

1

Hi You are making an extra iteration here As you are starting from the index which is equal to the array's length

for(var i = messagesParsed.length-1; i>=0; i--){
    message = messagesParsed[i];
    console.log(message);
}

//Otherwise, you can also use
for(var message of messagesParsed){
    console.log(message.content);
}

Comments

0

just replace var i = messagesParsed.length to var i = messagesParsed.length - 1 or better use forEach loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.