1

My query is:

connection.query("SELECT * FROM posts WHERE someId = 2", (err, rows) => {

    allIds = [];

            for(var i = 1; i <= rows.length; i++){
                allIds.push(rows.id_post)
            }

             res.send("The IDs : " + allIds )
});

rows.length = 5, I'm getting the response: The IDs : , , , , ,. There is no data. I need all the IDs listed. Later want to run another query with those Ids, maybe in the same query but for now why aren't the IDs showing? What am I missing?

2 Answers 2

1

It seems like 'rows.id_post' is undefined. Are you sure there is a property named 'id_post'? Is rows a json or an array? If it's an array you need:

for(var i = 0; i < rows.length; i++){
        allIds.push(rows[i].id_post)
    }

This should answer your question.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So what's causing i<=rows.length to fail and < to pass? Did that or i=0 is resolving this?
arrays are 0 indexed. so the object array[array.lenght] doesn't exist.
Can you help me with stackoverflow.com/questions/46302496/… please. Same query extended.
0
for(var i = 1; i <= rows.length; i++){
            allIds.push(rows.id_post)
        }

You're not referencing the index of the rows results... I'm not familiar with the Node.JS syntax but I would think you need to reference the i row to get the value to push...

2 Comments

You mean allIds.push(rows[i].id_post)? I tried that and get error : TypeError: Cannot read property 'id_comment' of undefined
try this: connection.query("SELECT * FROM posts WHERE someId = 2", function selectAll(err, rows, fields) {

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.