0

I have a nested array and I want to update that array with values in my database. It is based on a timetable: a 5 day week with 6 periods in each day. Monday is 0 and Friday is 4 in the array. There is period 0 up until period 5. When I execute this function it says: Cannot Set property '6' is undefined And my array is not updated When I remove the "return" line, the error does not occur but obviously my array is still not updated. Is this due to having multiple returns due to the loop. And if so how do i fix this? If it's another error please show me! If there is anymore information need please ask!

dayCountTest = 0;
    while (dayCountTest<5){
        console.log("2");
        if (dayCountTest == "0") {
            tempDay = "Monday";
        } else if (dayCountTest ===  "1") {
            tempDay = "Tuesday";
        } else if (dayCountTest == "2") {
            tempDay = "Wednesday";
        } else if (dayCountTest == "3") {
            tempDay = "Thursday";
        } else if (dayCountTest == "4") {
            tempDay = "Friday";
        }   

        periodCount = 0;
        while (periodCount < 6) {
            console.log("3");
            let db = new sqlite.Database('./linksdb.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
            let sql = `SELECT Link FROM `+ tempDay+`
                        WHERE Period = `+ periodCount + ``;

            db.all(sql, [], (err, rows) => {
                if (err) {
                    throw err;
                }
                
                rows.forEach((row) => {
                    return links[dayCountTest][periodCount] = row.Link;
                    
                });
            });
            db.close();
            periodCount++;
        }
        dayCountTest++;
    }
4
  • maybe you have a look to developer.mozilla.org/en-US/docs/Web/JavaScript/…, first Commented Jan 28, 2021 at 15:37
  • sorry for being stupid, but what exactly are you trying to tell me. ik im a noob lol Commented Jan 28, 2021 at 16:21
  • you take strict comparison === (same type, same value, ergo no type coercion) and check a number type with a string type. this is always false. please have a look to the various possibilities of checking a value in javascript. Commented Jan 28, 2021 at 16:28
  • still have no success with either === or == Commented Jan 28, 2021 at 16:59

1 Answer 1

0

I assume that you have initialized the array links someplace else, because I don't see it here. The way in which you are trying to assign values (for the first time) for the multidimensional array is incorrect. Each element inside the array isn't defined as a sub-array at first. Solved this problem using a simple for loop that iterates over the first 5 variables (in this case) and assigns them as empty variables. This can also be made dynamic depending on the nature of the problem.

Oh and lastly, you combined a return statement with an assignment statement. That's incorrect.

Check this link for more information.

    dayCountTest = 0;

    links = []; // declaring the array
    for (var i = 0; i < 5; i++) {
        links[i] = [,,,,,]; // making the first 5 elements of the link array as empty arrays of 6 length as there are six periods in this case
    }

    while (dayCountTest<5){
        console.log("2");
        if (dayCountTest == "0") {
            tempDay = "Monday";
        } else if (dayCountTest ===  "1") {
            tempDay = "Tuesday";
        } else if (dayCountTest == "2") {
            tempDay = "Wednesday";
        } else if (dayCountTest == "3") {
            tempDay = "Thursday";
        } else if (dayCountTest == "4") {
            tempDay = "Friday";
        }   

        periodCount = 0;
        while (periodCount < 6) {
            console.log("3");
            let db = new sqlite.Database('./linksdb.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
            let sql = `SELECT Link FROM `+ tempDay+`
                        WHERE Period = `+ periodCount + ``;

            db.all(sql, [], (err, rows) => {
                if (err) {
                    throw err;
                }
                
                rows.forEach((row) => {
                    links[dayCountTest][periodCount] = row.Link;
                    
                });
            });
            db.close();
            periodCount++;
        }
        dayCountTest++;

This fix should get this code working right. Do let me know the outcome of this.

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

10 Comments

got that bit working, now the problem is that it says it is undefined, even though the value is something else
Ok then. If you could just give this answer an upvote and accept it so it gets known, I would highly appreciate it.
logging the row.Link to the console after the line below, but nothing comes up, does this mean ive done somehting wrong with my database links[dayCountTest][periodCount] = row.Link;
Yeah, that's exactly what I suspected.
A suggestion. Use the sqlite3.OPEN_READONLY opening mode for applications that don't require access to write to the database. This is safer and keeps data protected. Also, doesn't give the application the right to create an entire database if there isn't one under the specified name.
|

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.