Cześć Utknąłem z jednym problemem, który wydaje się tak łatwy: chcę dodać nowe właściwości do istniejącego obiektu, ale nie działa to poprawnie tutaj mój kod:

 challengeSearchNearBy: async function (longitude , latitude,min_distance,max_distance) { 
    var challengeNearBy = await Challenge.find({
        location: {
            $near: {
                $geometry: { type: "Point", coordinates: [longitude, latitude] },
                $minDistance: min_distance,
                $maxDistance: max_distance                    
            }
        }
    });//limit(30);

    const challengesPromise = challengeNearBy.map(async function(o){
        const challenger_club = await Club.findById(o.challenger_club_id);
        const conqueror_club = await Club.findById(o.conqueror_club_id);

        o.challenger_club_name = challenger_club.club_name;
        o.challenger_club_avatar = challenger_club.avatar;
        o.conqueror_club_name = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name;
        o.conqueror_club_avatar = ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;

        console.log(o.challenger_club_name); // this line logged what I want (challenger_club_name)

        return o; // but this object not contain what I want (challenger_club_name, challenger_club_avatar ...)
    });
    const challenges  = await Promise.all(challengesPromise);
    return challenges;
},
-1
every Bit 11 listopad 2018, 06:08

1 odpowiedź

Najlepsza odpowiedź

Zwróć nowe obiekty na swojej mapie.

const challengesPromise = challengeNearBy.map(async function(o){
    const challenger_club = await Club.findById(o.challenger_club_id);
    const conqueror_club = await Club.findById(o.conqueror_club_id);

    return {
      ...o,
      challenger_club_name: challenger_club.club_name,
      challenger_club_avatar: challenger_club.avatar,
      conqueror_club_name: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.club_name,
      conqueror_club_avatar: ObjectHelper.isNullOrUndefined(conqueror_club)? "":conqueror_club.avatar;
    }
});
1
Ty Kroll 11 listopad 2018, 07:09