0

When I try to add status to participant, status is not available inside the participant. But when I log participant.status, it does contain the value.

May I know what the potential problem causing it?

exports.getMyInvitationParticipants = async function(req) {
    const invitation = await getMyInvitation(req)
    const participants = invitation.participants
    for (let index = 0; index < participants.length; index++) {
        var participant = participants[index]
        const member = await Member.findOne({ loginId : participant.phoneNumber })
        if (member) {
            const memberInvitation = await MemberInvitation.findOne({ memberId : member._id })
            if (memberInvitation) {
                participant.status = memberInvitation.status

                console.log(participant.status)
                console.log(participant)
            }
        }
    }
    return participants
}

router.get('/invitation/my/:invitationId/participants', authenticate, async (req, res) => {
    try {
        const participants = await invitationController.getMyInvitationParticipants(req)
        return res.send(participants)
    } catch (err) {
        return res.send(err)
    }
})

console.log output

PENDING <---- participant.status
{
  _id: 5ce113e21c6fc1549c4221ad,
  name: 'Dummy name'
}
Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
Anders Cheow
  • 107
  • 10
  • It is because of the Asynchronous behavior of your function. Here is a great explanation, https://stackoverflow.com/a/23667087/7568566 – Tan-007 May 19 '19 at 10:14
  • There is no asynchronous behavior -- the calls are synchronized with `await`, that's what it's for. The `getMyInvitationParticipants` may be returning a promise but its statements execute synchroniously and `await` puts everything in the right order. In fact, I've mocked the code and it works fine in my case -- I get a status value and everything. Please post your auxiliary functions -- those `getMyInvitationParticipants` calls. If they're too large try to simplify them for the sake of your own debugging, at least, before posting them. – Armen Michaeli May 19 '19 at 10:32
  • @Tan_007 I've read the explanation. But in the end, what will be the solution to my problem? – Anders Cheow May 19 '19 at 10:34
  • @amn I've updated to the post :) – Anders Cheow May 19 '19 at 10:37
  • I would like to see definitions of `getMyInvitation`, `Member`, `MemberInvitation` in order to be able to look at this in depth. Also, take a look at https://stackoverflow.com/help/reprex -- you don't have to post your entire application if you can post a minimal version of it that replicates your problem. – Armen Michaeli May 19 '19 at 10:42
  • What is `participant` ? Is it a normal object ? – Titus May 19 '19 at 10:42
  • @amn Member and MemberInvitation, and getMyInvitation are just Mongoose's model – Anders Cheow May 19 '19 at 10:46
  • @Titus Yes, participant is just a normal object – Anders Cheow May 19 '19 at 10:47

2 Answers2

0

Given the result as pending, I believe memberInvitation.status to be an unresolved promise.

So, try adding await before the memberInvitation.status as you want the value and not the promise itself.

    if (memberInvitation) {

        participant.status = await memberInvitation.status

Updated code :

exports.getMyInvitationParticipants = async function(req) {
const invitation = await getMyInvitation(req)
const participants = invitation.participants
for (let index = 0; index < participants.length; index++) {
    var participant = participants[index]
    const member = await Member.findOne({ loginId : participant.phoneNumber })
    if (member) {
        const memberInvitation = await MemberInvitation.findOne({ memberId : member._id })
        if (memberInvitation) {
            participant.status = await memberInvitation.status

            console.log(participant.status)
            console.log(participant)
        }
    }
    }
    return participants
}
router.get('/invitation/my/:invitationId/participants', authenticate, async (req, res) => {
    try {
        const participants = await invitationController.getMyInvitationParticipants(req)
        return res.send(participants)
    } catch (err) {
        return res.send(err)
    } })
Hiteshdua1
  • 2,126
  • 18
  • 29
0

It turns out, schema of invitation.participants lack of status. So when I try to assign status to participant, it does not work.

I've come out a solution to solve that.

exports.getMyInvitationParticipants = async function(req) {
    const invitation = await getMyInvitation(req)
    const participants = invitation.participants.map((participant) => {
        return { 
            _id : participant._id,
            name : participant.name,
        }
    })
    for (let index = 0; index < participants.length; index++) {
        const participant = participants[index]
        const member = await Member.findOne({ loginId : participant.phoneNumber })
        if (member) {
            const memberInvitation = await MemberInvitation.findOne({ memberId : member._id })
            if (memberInvitation) {
                participant.status = memberInvitation.status
            }
        }
    }
    return participants
}
Anders Cheow
  • 107
  • 10