0

I think I am doing something wrong, I am not able to send value to frontend. res.json(arr) this line of code give [] blank array. but console.log(arr); gives all value what I needed(JSON value).

Code

    var arr = [];
    var messagesPromise = new Promise((resolve, reject) => {
        messages.forEach(function (val,index) {
            arr[val.session] = val;
            if (index === messages.length -1) resolve(arr);
        });
    });


    messagesPromise.then((arr) => {
        console.log("######################");
        console.log(arr);  // this line of code gives json value 
        console.log("######################");

        res.json(arr); // this line of code gives [] array
    });
Anudeep GI
  • 931
  • 3
  • 14
  • 45
  • 1
    Ahhh, I see the problem ... give an example of `messages` array, I bet the objects have `session` properties that are not numeric – Bravo Oct 23 '19 at 10:42
  • 1
    What you need to do is `var arr = {}` - because you're creating an **object** not an **Array** - note, you can add whatever properties you like to an **Array**, but they will be ignored when converted to JSON – Bravo Oct 23 '19 at 10:44
  • Please share example of **messages** array also. – Swapnil N Oct 23 '19 at 10:44
  • 1
    @SwapnilN - I bet the objects in messages have `session: "not a numeric value"` – Bravo Oct 23 '19 at 10:44

0 Answers0