0

I am trying to run a put function to update a user in my mongoDB, but I am getting a 500 Internal Service error. I am using the angular-fullstack generator

I use this resource in my controller:

  update(User) {
    User.$update();
  }

Here are the functions I call in the backend to try and put the data everything seems to be working fine except for when I call saveUpdates, it seems like that is triggering the 500 error.:

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}
// this seems to be the function giving me the problem. 
function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.saveAsync()
      .spread(updated => {
        return updated;
      });
  };
}

function respondWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

export function updateUser(req, res) {
  if (req.body._id) {
    delete req.body._id;
  }
User.findByIdAndUpdate(req.params.id)
    .then(handleEntityNotFound(res))
    .then(saveUpdates(req.body))
    .then(respondWithResult(res))
    .catch(handleError(res));
    }

I have tried following recommendations on this page for angular-full stack, such as changing ._merge to ._extend to no avail.

Rarepuppers
  • 723
  • 1
  • 10
  • 21

1 Answers1

1

I answered my question:

Angular-full stack utilizes lodash, so if you want to make put requests using ._merge like this:

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

then you need to import lodash into the user.contoller set up by the angular-fullstack yeoman generator.

import _ from 'lodash';
Rarepuppers
  • 723
  • 1
  • 10
  • 21
  • Oh man, I had been trying to figure out why I was getting a 500 error after a merge, and I completely overlooked the exclusion of lodash! It's the little things... – Drian Naude Jun 24 '16 at 00:07