enbermudas
enbermudas

Reputation: 1615

Dynamically updating single field in model

I'm using adonisjs and there is a problem with my update method:

async update(id, data) {
  const user = await User.find(id);
  user.merge(data);
  return await user.save();
}

For some reason, the merge method doesn't modify the provided fields (data is an object with the fields I want to update and its new values) but updates everything... including the hashed user's password which is a big problem for me. I don't want to do such a thing, just update the provided fields. Is there a way to fix it?


I've stoped the application, served it once again and created a fresh new database just to be sure. It keeps updating fields that shouldn't be edited. Later, if the user tries to log in, it gets the next error message (obviously):

{
    "message": "auth/login/error",
    "error": "E_PASSWORD_MISMATCH: Cannot verify user password"
}

Images displaying the user's password being updated:

Before the update: Before being updated

After the update: After being updated

Upvotes: 1

Views: 563

Answers (1)

devcass
devcass

Reputation: 801

What's up?

So, you can just update values instead of replacing it, for example:

  user.name = "foo"

in the end

  await user.save()

Upvotes: 1

Related Questions