Search code examples
loopbackjs

Issue in loopback's upsertWithWhere()


I'm using loopback3.x. Why upsertWithWhere function always updates the same instance? Only one instance is there for all the time when updateWithWhere function executes.

app.models.oneTimePassword.upsertWithWhere({
    where: {
        userId: user.id
    }
}, {
    userId: user.id,
    otp: otp,
    updatedAt: updatedAt,
    type: 'email'
}, (err, res) => {
    if (!err) {
        callback(null, {
            status: "OK",
            message: "email sent"
        });
    } else {
        callback(err);
    }
});

Solution

  • app.models.oneTimePassword.upsertWithWhere(
      {
        userId: user.id
      },
      {
        userId: user.id,
        otp: otp,
        updatedAt: updatedAt,
        type: 'email'
      },
      (err, res) => {
        if (!err) {
            callback(null, {
                status: "OK",
                message: "email sent"
            });
        } else {
            callback(err);
       });
    

    Try this, The first argument of upsertWithWhere should be where therefore, you don't need to add where: {} check out this official documentation