I have a code, all the code is workin except the for-loop

let query = args.slice(1).join(' ');

client.database.query(`SELECT * FROM centres WHERE name="${query}"`, function (error, results, fields) {
    if (error) throw error;
    if (!results[0]) {
        message.channel.send(msg.cs.notExist)
    } else {
        client.database.query(`SELECT * FROM vehicules WHERE centre="${query}"`, function (err, res, field) {
            if (err) throw err;
            console.log(res.length)
            let i;
            for (i = 0; i === res.length; i++) {
                message.channel.send(res[i].name)
                console.log(i)
            }
            /*   message.channel.send(`${msg.cs.stats}${query}

            ${msg.abreviation.cdc}${client.functions.username(client, results[0].cdc)}
            ${msg.abreviation.cdca}${client.functions.username(client, results[0].cdca)}
            ${msg.cs.vehicule} ${results[0].vehicules}`)*/
        })
    }
});

I just have 4 in output for res.length

3 Answers

3
ellipsis On Best Solutions
i=0; i===res.length; i++

The condition in the for loop is contradicting. Initially i is 0 and the condition for the loop is given such that i is always equal to res.length, then after each iteration i++ will increment i. Suppose the length of res is 7. Due to that condition the loop will never run to begin with because i is 0. If i<res.length was the condition then it will work fine.The loop will look like

for (i = 0; i < res.length; i++)
2
Kamil Naja On

For me, you should use <= or < inside loop

for(i=0; i <= res.length; i++){
  message.channel.send(res[i].name)
  console.log(i)
}
2
AlexGera On

i = 0 initially while res.length = 4. The loop terminates immediately. Change it to i < res.length