0

I am generating a key-value pair array in my code, but when I pass the array in res.json() I am receiving null on front-end.

This is the key-value array that I want to pass. Any Idea what I am not doing right?

router.post('/xlsData',  session, async (req, res)=>{

    const reqStatus = req.body.searchType;
    const reportDate = req.body.reportDate;
    const userBebitReq = await debitReq.find({ reqStatus : reqStatus,  reqType: "Debit", reqDate : reportDate},{_id : 1, userId: 1,reqAmount : 1, withdrawalMode: 1, reqDate: 1, });
    let userIdArray = [];
    let debitArray = [];
    for(index in userBebitReq)
    {
        let reqAmount= userBebitReq[index].reqAmount;
        let withdrawalMode= userBebitReq[index].withdrawalMode;
        let reqDate= userBebitReq[index].reqDate;
        let user = userBebitReq[index].userId;
        let rowId = userBebitReq[index]._id;
        let userKi = mongoose.mongo.ObjectId(user);
        userIdArray.push(userKi);
        debitArray[userKi] = {
            rowId : rowId,
            userId : userKi,
            reqAmount: reqAmount,
            withdrawalMode: withdrawalMode,
            reqDate: reqDate
        }
    }

    let user_Profile = await userProfile.find({ userId: { $in: userIdArray } });

    for(index in user_Profile)
    {
        let id = user_Profile[index].userId;
        if(debitArray[id]){
            debitArray[id].address = user_Profile[index].address;
            debitArray[id].city = user_Profile[index].city;
            debitArray[id].pincode = user_Profile[index].pincode;
            debitArray[id].name = user_Profile[index].account_holder_name;
            debitArray[id].account_no = user_Profile[index].account_no;
            debitArray[id].bank_name = user_Profile[index].bank_name;
            debitArray[id].ifsc = user_Profile[index].ifsc_code;
            debitArray[id].paytm_number = user_Profile[index].paytm_number;
        }
    }

    res.json({
        status : 1,
        data : debitArray
    })  
})

Receiving this on front-end : data: Array(0)

4
  • Did you try debugging ? Because this should do the work Commented Nov 27, 2019 at 10:58
  • yes i have tried got no error in debugging Commented Nov 27, 2019 at 11:01
  • is res.json() line executed after all above operations are done? I think maybe an issue of asynchronous behavior Commented Nov 27, 2019 at 11:05
  • got answer below, and i guess no problem with asynchronous thank you for the response. Commented Nov 27, 2019 at 11:10

1 Answer 1

2

Do it like this:

let debitArray = {};

instead of this.

let debitArray = [];

You are basically using object not array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.