1

Setting:

  • Strongloop / loopback: ^2.10.2
  • AngularJS 1.3

Problem:

On the profile page the user model gets updated, but the password should not be transferred (anyway it is hashed and not available.).

Approach:

So I tried the following.

user.$save().then(cb);

the user-object is a $ressouce and it does create a PUT /users/?id=1 request to the server, but strongloop tries to INSERT the entity instead of updating, and the error pops up:

"message":"ER_NO_DEFAULT_FOR_FIELD: Field 'password' doesn't have a default value"

Question:

How can I tell strongloop to make an update in the PUT and not an insert?

Thank you!

Simon Fakir
  • 1,712
  • 19
  • 20
  • 1
    Btw. same behaviour with "User.upsert(user, cb);" – Simon Fakir Mar 10 '15 at 22:03
  • This problem has also a github issue. Unbelievable, that this basic problem is not solved yet. -_- https://github.com/strongloop/loopback-sdk-angular/issues/125 – Simon Fakir Mar 10 '15 at 22:22
  • Well, not that unbelievable, given that this is not a core feature of loopback (although a very convenient one), that most likely requires quite some debugging and that time is a limited resource :) – Overdrivr Nov 22 '16 at 14:11

3 Answers3

0

Found a really bad solution, which should only be used, until the real fix is in production. It is bad configured and duplicated code, but at least it works.

var urlBase = "/api";
var UserUpdate = $resource(
    urlBase + '/users/:id',{id:'@id'},
    {"upsert": { url: urlBase + "/users/:id", method: "PUT"}}
 );
 UserUpdate.upsert({id: user.id}, user, cb, cb);
Simon Fakir
  • 1,712
  • 19
  • 20
  • Have you even bothered looking at the other answers? – Overdrivr Dec 14 '16 at 13:24
  • 1
    Back then there was no other answer :) but thank you for the note – Simon Fakir Dec 14 '16 at 13:47
  • Ah ok. Sorry if I sounded harsh, did not mean. I just relayed the fix provided by one of the core devs, so you should consider trying it. And please consider selecting any of the answers as accepted to close the question if you feel it's solved. Cheers – Overdrivr Dec 14 '16 at 15:24
0

Below code is worked for me, hope it will help.

var params = {
                access_token: LoopBackAuth.currentUserToken,
                id:LoopBackAuth.currentUserId
            };
            var promise = User.prototype$updateAttributes(
                params,
                user,
                angular.noop,
                angular.noop
            ).$promise;

            promise.then(ok,fail);
0

A workaround was provided here.

So in your code replace the call to user.$save by the following

userData = {'someCustomUserProperty':'someValue'};
User.prototype$updateAttributes({ id: user.id }, userData);
Overdrivr
  • 6,296
  • 5
  • 44
  • 70