angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.12k stars 1.24k forks source link

PUT on the default endpoint generator broken #2205

Open rufogongora opened 8 years ago

rufogongora commented 8 years ago

I just generated an endpoint with the generator and I'm getting some weird functionality on it, doing a PATCH request won't work at all and PUT does some weird stuff like it updates some field of other models.

I'm using an AngularJS $resource PUT request, for example my resource would look like this: return $resource('/api/thing/:id', {id: '@_id'},{ 'update' : {method:'PATCH'} })

After that on the controller I would do:

this.thing.$update((data)=>{console.log(data)})

What I ended up doing was that I went back to the lodash approach instead of jsonpatch and it worked for me.

Instead of using the jsonPatch "PatchUpdates" function I'm using this one

function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.save()
      .then(updated => {
        return updated;
      });
  };
}

While this is working I'm wondering if the issue is with jsonpatch itself or otherwise, because I'm using the out of the box upsert function and it messes my other data, or is my understanding of upserting wrong?

Awk34 commented 8 years ago

The HTTP PATCH follows the specs for the HTTP PATCH method: https://tools.ietf.org/html/rfc5789, https://tools.ietf.org/html/rfc6902.

"I'm using an AngularJS $resource PUT request, for example my resource would look like this: return $resource('/api/thing/:id', {id: '@_id'},{ 'update' : {method:'PATCH'} })" - that's not a PUT, you're manually telling it to use PATCH here..

Awk34 commented 8 years ago

Here's a simple article about HTTP PATCH: http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/

rufogongora commented 8 years ago

@Awk34 Sorry about the confusion on the $resource declaration, I was switching back and forth from update to patch to test what was the issue.

Well my conventional method with the generator's version before 4.0.0 was to simply modify a value of an angular resource (after getting it) then simply call the $update method as declared on the first comment (with a PUT verb instead).

When I tried this on the newest version, some of my other values in the db were being updated for no reason, replacing their values with some of the values of the resource I was trying to update. Then I noticed on the server controller, the PUT verb is actually doing an upsert, which I didn't want, since I was just trying to modify a simple value, that's why I moved to PATCH, however patching was not updating anything on the DB and simply returning a 200.

I assumed the error was with jsonpatch, I moved to lodash and this solved my issue.

andrewt3000 commented 8 years ago

I also had the problem with the put. Here is how I resolved it. In the upsert method...

return Link.findOneAndUpdate({_id: req.params.id}, ...