-1

Hello I have the following function

update: (id) => {
      let updated = false
      
      database.users = database.users.map((user) => {
        if(user.id === id){
          updated = true
          user.active = true;
        }
        return user
      })

      return updated
    }

As you can see the function iterates over an array in order to update the attribute user.active, and update the updated variable declared outside.

I debugged this and the function enter inside the if, and update the value, but always return updated = false.

How can I do to update that variable?

I am using node v14.16.1, with babel:

{
    "presets": [
        "@babel/preset-env"
    ]
}

UPDATED

  • The database is a JSON object is not asynC
  • The database can have more than one user with the same id

The questions about why updated don't is updated inside the arrow function.

Thanks.

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
  • Wait, what's the asynchronous function here? – Pointy May 15 '21 at 15:27
  • 3
    You 're using `.map()`, but your callback function does not return anything. Thus `database.users` will end up being an array of `undefined` values. Instead, you should use `.some()` and the callback should update the user and then return `true`, and that should be the return value from the outer function. – Pointy May 15 '21 at 15:29
  • 2
    Databases are very typically async. If it wasn't, and that was a normal sync `Array.prototype.map`, then "I debugged this and the function enter inside the if, and update the value, but always return updated = false." would not make any sense. In the latter case, a "missing debugging details" would be appropriate, but i think taking the far more likely scenario here is a better choice. – ASDFGerte May 15 '21 at 15:31
  • @ASDFGerte has a point: the function database.users.map() must be asynchronous, thus it changes the "update" status after the false value had already been returned in the (id)=>{...} call – Iiro Ullin May 15 '21 at 15:48
  • @ASDFGerte well I admit that it's within the realm of possibility that the `.map()` is *not* the normal Array prototype `.map()`, but that would be kind-of weird in my opinion. – Pointy May 15 '21 at 15:48
  • 1
    @IiroUllin that, or else none of the "user" objects in the array match the id. It would of course be extremely helpful if the OP included some clue as to what `database` and `database.users` are. – Pointy May 15 '21 at 15:49
  • @Pointy, but OP said that the if(user.id === id){...} clause is executed, so some must have matched.... – Iiro Ullin May 15 '21 at 15:51
  • It is explicitly stated, that debugging the code, the `if`-statement is entered, so that case is, as mentioned above, not possible - unless some information of the question is straight up wrong. Here, as the question got closed rather quickly, i can somewhat understand, that OP is now unresponsive. I'd still just go with my glass ball of "async call", but people seem to disagree, and i won't stop them, if they think they have a better answer. – ASDFGerte May 15 '21 at 15:56
  • @ASDFGerte I don't disagree that it *might* be asynchronous, and that of course would explain everything, but to me it doesn't *look* asynchronous; it looks like an ordinary (misuse of) `.map()`. But who knows? – Pointy May 15 '21 at 15:59
  • I don't think there is a possible situation, where that is `Array.prototype.map`, and the statement about the debugging also holds. – ASDFGerte May 15 '21 at 16:07
  • Hi, the database is a JSON object is not async, and I solved the returning user into the function and could be more than one user with the same id, the questions about why update doesn't is updated inside the arrow function. – Tlaloc-ES May 15 '21 at 16:11
  • K, in the light of what @Tlaloc-ES added to the inquiry, I can only see one more possibility: if the top-level function, update: (id) => ... is itself defined on database.users, then it will be overwritten after the database.users.map(...) call. If that's not the case, I'd like to see a minimal working example reproducing this problem... – Iiro Ullin May 15 '21 at 16:42

1 Answers1

1

First of all you are not returning anything in map function. Also there is no point to map over entire array, while you want to update just 1 element.

const database = {
  users: [
    {id: 1, name: "John" , age: 15, active: false},
    {id: 2, name: "Bob"  , age: 18, active: false},
    {id: 3, name: "James", age: 25, active: true },
    {id: 4, name: "Tom"  , age: 21, active: false},
    {id: 4, name: "Karen", age: 19, active: false},
  ]
}

const updateOne = id => {
    const user = database.users.find(e => e.id === id)
    if (user) user.active = true
    return !!user
}

const updateAll = id => {
    const users = database.users.filter(e => e.id === id)
    users.forEach(e => e.active = true)
    return !!users.length
}
    
console.log(updateOne(2))
console.log(database)
console.log(updateAll(4))
console.log(database)
ulou
  • 5,542
  • 5
  • 37
  • 47
  • "*you want to update just 1 element*" - who knows? There might be multiple objects in the array with a matching `id` :-) But sure that's a reasonable assumption to make. – Bergi May 15 '21 at 15:54
  • Btw I'd use `find` instead of accessing the array by index. – Bergi May 15 '21 at 15:54
  • 1
    @Bergi You are right, it's even more elegant to update by reference. I've updated the answer. – ulou May 15 '21 at 15:57
  • Could be find more than one user, the questions is mor about why the updated variable is not updated. – Tlaloc-ES May 15 '21 at 16:12
  • @Tialoc-ES is your database `id` a `string` or `number`? Try to change condition into: `user.id == id` or `+user.id === +id` – ulou May 15 '21 at 16:19