Koa2 queries the result object of the database and adds new attributes, but the front end cannot get it asynchronously.

1. First of all, get the order list through the query, and then add the user name and user password for each order object through the for loop. When I console.log, the console has output, but when I, ctx.body = {data:orderList}, the result obtained asynchronously on the front-end page does not have user name and user attributes. Please see the following figure
background koa2 code:

clipboard.png

:

clipboard.png

Why do you print it out at the back end and get it asynchronously without the user_name and user_headurl attributes I added? Ask for help

Mar.02,2021

after I use: JSON.stringify and JSON.parse to execute the returned object, I can add properties to this object, but I'm still not clear about the specific reason.
the latest modification:
find the API of Sequelize and found that there is a setDataValue method that can be used to query the returned object settings. Just change the code of the original problem to the following:

for (let i = 0; i < orderList.length; iPP) {
    let userInfo = await wdUser.findById(orderList[i].user_id)
    orderList[i].setDataValue('user_name',userInfo.user_name)
    orderList[i].setDataValue('user_headurl',userInfo.user_headurl)
}

API link: https://itbilu.com/nodejs/npm.


add a raw: true condition to the query, and you can assign the value directly.


mongoose overrides the toJSON method. Properties you add later will not be returned in the toJSON method. That is to say,


you need to convert document object to javascript object
link

.
  • Koa interface cross-domain error report

    several interfaces are written with koa+mongodb. Cross-domain errors are always reported when requesting with ajax, even if cors is used in koa. Google has done a lot of things but still hasn t solved the problem . const cors = require( koa2-cors ); ...

    Feb.28,2021
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-1e4265e-449a8.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-1e4265e-449a8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?