节点未返回正确的JSON响应

时间:2018-04-19 08:31:57

标签: javascript node.js

我有一个对象,我想作为回复返回,但在此之前我想添加几个字段。当我添加字段并打印对象时,它会打印新对象,但是当我发送响应时,我仍然会在编辑之前获得旧对象。

let applications = await ApplicationHandler.getApplicationOverview(application_id); //I want to edit this one

if (applications == null) {
    applications = [];
}
for (let i = 0; i < applications.length; i++) {
    let cube_application_id = applications[i].id;
    let application_ratings = await Application_Rating.findAll({where: {cube_application_id: cube_application_id}});
    let application_ratings_personal = await Application_Rating.findAll({
        where: {
            cube_application_id: cube_application_id,
            user_id: req.user.id
        }
    });
    let total_user_rating = 0;
    for (let rating of application_ratings) {
        let average_user_rating = (rating.personality_rating + rating.qualification_rating + rating.motivation_rating) / 3;
        total_user_rating = total_user_rating + average_user_rating;
    }
    applications[i].average_rating = total_user_rating / application_ratings.length;
    applications[i].recommended = application_ratings_personal.recommended;

    if (i == applications.length - 1) {

        console.log(applications); // this prints with those 2 fields.

        let successRes = {
            status: 200,
            data: applications
        };

        return successRes; //however, this returns the one which I had in the first line.
    }
}

0 个答案:

没有答案