Json array traversal modifies attribute value to report Cannot read property 'rank' of undefined?

ask the bosses, I for in traversal to modify the json array will report a undefined error, can you help me to see what the problem is?

var res = await mysql("cMyStarInfo").where("open_id", id).orderBy("Djlsh", "desc");
for(i in res){
  //var newRes = res[i];
  var a = res[i].PenName;
  var b = await mysql("cPenInfo").where("penName", a);
  var c = await mysql("cCoures_list").where("penName", a).orderBy("Djlsh", "desc");;
res[i].rank = b[0].rank
res[i].PenImgUrl = b[0].roundImgUrl
res[i].StarSum = b[0].starSum
res[i].upCourTitl = c[0].couTitle   
}
ctx.state.data = res


clipboard.png

for


clipboard.png

Jan.31,2022

you can't print rank if rank is empty, that is undefined


the returned data is posted


it feels like you b [0] where the rank may not be defined, or you can step by step debug and check what is wrong with the syntax
.


if your res gets an array and you want to change the value or extension of the data objects in the array, it is recommended that you use

//  res.map 
res.forEach(item => {
    // 
    if(!item) return;
    // 
    ...
});
// res

try not to fo..in the array (for, for..of, and array interfaces are recommended), because the array is also an object, and you can add non-array index fields, such as

.
const ar = [{id: 23}];
ar.b = undefind;
//ar.length1;
for (var k in ar) console.info(k); 
// 
// => 0,b
// k==='b'undefined;
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b380af-2c0d6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b380af-2c0d6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?