0

I recognize that this has been talked about, but none of the given answers are working. This is an async function.

const activeOrders = await Order.getActiveOrders()
const orderIds = activeOrders.map((order) => { return order.clientId })
const fullClients = await clientController.getClientsByIdArray(orderIds)
for (let j = 0; j < activeOrders.length; ++j) {
    for (let i = 0; i < fullClients.length; ++i) {
        if (String(activeOrders[j].clientId) === String(fullClients[i]._id)) {
            activeOrders[j]['client'] = fullClients[i] // THIS IS NOT WORKING
            console.log(activeOrders[j].client) // THIS CORRECTLY DISPLAYS fullClients[i]
            console.log(activeOrders[j]) // THIS SHOWS THE OBJECT BUT DOES NOT 
                                         // INCLUDE activeOrders[j].client
        }
    }
}

console.log(activeOrders[0]) // HERE activeOrders[j].client IS MISSING (there is currently 1 item in each array)

const arr1 = [{stuff: "Some stuff", id: 6}]
const arr2 = [{otherIsh: "Sysreq", id: 6}]

for (let i = 0; i < arr1.length; ++i) {
  for (let j = 0; j < arr2.length; ++j) {
    if (arr1[i].id === arr2[j].id) {
      arr1[i].newObj = arr2[j]
      break
    }
  }
}

console.log(arr1[0])

I really don't get how it can be there in the first console.log, but then not exist in the next. In this snippet, I do the same thing and it works just fine.

insite
  • 53
  • 7
  • Your code seems totally fine. Does`fullClients[i]` destroy in somewhere? `activeOrders[j].client` and `fullClients[i]` are same copy if one of them is destroyed then another will also be destroyed. – Pylon Jul 21 '20 at 05:41
  • Try using `console.log(JSON.stringify(...))`. When you log an object, the console keeps a live reference to it, and updates what it displays as the object changes. Logging JSON will make a snapshot of the object contents. – Barmar Jul 21 '20 at 05:42
  • See https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log – Barmar Jul 21 '20 at 05:43
  • @Pylon I actually thought of that and already tried assigning { ...fullClients[i] } to counteract a destruction. Sadly, it did not work. – insite Jul 22 '20 at 02:09
  • @Barmar I appreciate your help. The problem is not in the logging as I return the new object in a json to the client. (this is part of some server code in an API). Therefore, regardless of how console.log reacts to various input, the end object should have been returned correctly. I didn't know that about console.log though, thanks. – insite Jul 22 '20 at 02:12
  • I suspect that JSON.stringify would work, but I'd rather not have to pass it as a string to this object before sending the whole object, because then I would have to explicitly parse it on the client side. – insite Jul 22 '20 at 02:14
  • I was only suggesting using it in the `console.log()`, not passing it around. If that's not the problem, then I'm really not sure what your problem is. – Barmar Jul 22 '20 at 03:26
  • Asynchronous code is notorious for causing this issue with console.log. – Barmar Jul 22 '20 at 03:28
  • Humor me and change the code to `console.log(JSON.stringify(activeOrders[j]))` and see if it fixes it. – Barmar Jul 22 '20 at 03:29
  • Did you wrapped the results of `Order.getActiveOrders()` by any order? I mean something like `api.get(url, result => result.map(item => new ModelName(item)));` – Chuong Tran Jul 22 '20 at 03:49

1 Answers1

0

Solved it. I didn't realize this had anything to do with Mongoose. Here is the link to the question with the correct answer. Unable to add properties to js object

insite
  • 53
  • 7