添加到对象时,其他键/值对不出现

时间:2019-05-19 10:07:49

标签: javascript node.js mongoose

当我尝试将status添加到participant时,参与者内部状态不可用。但是,当我登录participant.status时,它确实包含该值。

我可以知道是什么引起的潜在问题吗?

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输出

PENDING <---- participant.status
{
  _id: 5ce113e21c6fc1549c4221ad,
  name: 'Dummy name'
}

2 个答案:

答案 0 :(得分:0)

鉴于结果为待定,我认为memberInvitation.status是未解决的promise

因此,请尝试在await之前添加memberInvitation.status,因为您想要的是值而不是承诺本身。

    if (memberInvitation) {

        participant.status = await memberInvitation.status

更新的代码:

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)
    } })

答案 1 :(得分:0)

事实证明,invitation.participants的架构缺少status。因此,当我尝试将status分配给participant时,它将不起作用。

我已经提出了解决方案。

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
}