-1

I am using Node.js, Express and MethodOverride to try and have a form update only 1 part of a model (my user model).

User model:

var userSchema = new mongoose.Schema({

email: { type: String, unique: true, lowercase: true },
  password: String,

  profile: {
    name: { type: String, default: 'Company Name' },
    location: { type: String, default: 'Location' },
    website: { type: String, default: 'Your Website' },
    picture: { type: String, default: '' }
  },

  assetNumPre: { type: String, default: 'test' }, // this is the one I want to change

});

module.exports = mongoose.model('User', userSchema);

HTML form:

<form role="form-inline"action="/dashboard/settings/assetNumber?_method=PUT" method="POST">
 <div class="col-md-3">
   <div class="form-group">
     <label for="prefix" class="control-label">Prefix for Asset Number</label>
     <br>
     <small>Any alphanumeric characters to a limit of 6</small>
     <input type="text" class="form-control" id="prefix" name="prefix" placeholder="Prefix max 6 characters" maxlength="6" value="{{ prefix }}">
   </div><!-- Prefix for Asset Number-->
   <br>
   <div class="box-footer">
     <button type="submit" class="btn btn-primary">Submit</button>
   </div>
  </div>
</form>

Then route:

app.put('/dashboard/settings/assetNumber',
setRender('dashboard/settings/assetNumbers'),
setRedirect({auth: '/login'}),
isAuthenticated,
dashboard.getDefault,
(req, res) => {
var prefix = req.body.prefix;
console.log(req.params);
User.findByIdAndUpdate({_id: req.params.user_id}, prefix, function(err, UpdatedUser) {
  if (err) {
    res.send(err);
  }
  console.log(UpdatedUser);
  });
res.locals.prefix = req.user.assetNumPre;
});

One thing my route is missing is req.user.assetNumPre which is where I need to save it but I have no clue how to do this PUT request. Docs are not helping much either.

I got the route from a Stack Overflow example a few days ago and can't find the link to it. My app.js had method override working because I have done DELETE requests already. The model has the correct field and has a default test value that shows up in my show page.

Kirbytech
  • 634
  • 1
  • 5
  • 18

2 Answers2

1

You're calling this:

User.findByIdAndUpdate({_id: req.params.user_id}, prefix...

But prefix is only the value:

var prefix = req.body.prefix;

findByIdAndUpdate takes an Object, not a value, to update a specific field.

So try:

User.findByIdAndUpdate({_id: req.params.user_id}, { assetNumPre: prefix }...
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • Thanks I got it to work by going back to some old code and taking an example from there. I will post my new route as an answer but yours is the solution to my problem I just saw it after I figured it out. – Kirbytech Nov 05 '17 at 16:27
0

Here is the fixed route:

app.put('/dashboard/settings/assetNumber',
    setRedirect({auth: '/login', success: '/dashboard/settings/assetNumber', failure: '/dashboard/settings/assetNumber'}),
    isAuthenticated,
    (req, res) => {
      User.findById(req.user.id, function(err, user) {
      if (err) return (err);
      user.assetNumPre = req.body.prefix || 'pre';

      user.save(function(err) {
        if (err) return (err);
          req.flash('success', { msg: 'Asset Number Prefix updated.' });
          res.redirect(req.redirect.success);
      });
    });

    res.locals.prefix = req.user.assetNumPre;
    });

So a few things changed that were not part of the issue. I figured out I need to just set the data inside the callback function. Then do a user.save.

Kirbytech
  • 634
  • 1
  • 5
  • 18