1
votes

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!

3
Btw. same behaviour with "User.upsert(user, cb);" - Simon Fakir
This problem has also a github issue. Unbelievable, that this basic problem is not solved yet. -_- github.com/strongloop/loopback-sdk-angular/issues/125 - Simon Fakir
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

3 Answers

0
votes

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);
0
votes

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
votes

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);