简体   繁体   中英

Returning multiple values in an array

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++;
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM