weixin_33726943 2016-09-28 12:08 采纳率: 0%
浏览 39

更新Node.js Passport用户

A Node.js / Express app with MongoDB, and using Passport, Passport Local and Passport Local Mongoose.

I'm trying allow registered users of my site to update their profiles. My idea was to rehash the signup form and logic, and send the updated data via a PUT request to the server.

The signup uses Ajax to submit the form, and whilst that works OK, when I send a PUT request to update the user, req.body comes back empty and the server throws out an error 500.

The update-form markup and javascript are nearly identical to the signup, so is it because I'm using a PUT request? I'm not even sure if I'm going about this in the right way...

Any pointers would be very happily received!

Edit user form submit logic:

    $form.on('submit', function(e) {
      if ($form.hasClass('is-uploading')) return false;

      $form.addClass('is-uploading').removeClass('is-error');

      if (isAdvancedUpload) {
        e.preventDefault();

        var ajaxData = new FormData($form.get(0));

        if (droppedFiles) {
          $.each(droppedFiles, function(i, file) {
            ajaxData.append($input.attr('name'), file);
          });
        }

        $.ajax({
          url: $form.attr('action'),
          type: $form.attr('method'),
          // data: ajaxData,
          dataType: 'json',
          cache: false,
          contentType: false,
          processData: false,
          complete: function() {
            $form.removeClass('is-uploading');
          },
          success: function(data) {
            // $form.addClass(data.success == true ? 'is-success' : 'is-error');
            // if (!data.success) console.log(data);
            window.location.replace('/matches');
          },
          error: function(xhr, textStatus, errorThrown) {
            console.log(xhr)
            console.log(xhr.statusText);
            console.log(textStatus);
            console.log(errorThrown);          }
        });

      } else {
        var iframeName = 'uploadiframe' + new Date().getTime();
        $iframe = $('<iframe name="' + iframeName + '" style="display: none;"></iframe>');

        $('body').append($iframe);
        $form.attr('target', iframeName);

        $iframe.one('load', function() {
          var data = JSON.parse($iframe.contents().find('body').text());
          $form
            .removeClass('is-uploading')
            .addClass(data.success == true ? 'is-success' : 'is-error')
            .removeAttr('target');
          if (!data.success) $errorMsg.text(data.error);
          $form.removeAttr('target');
          $iframe.remove();
        });
      };
    });

Server Side Edit Route:

// PUT edits
app.put('/users/:_id', function(req, res){
  var spokenLangs = req.body.spokenlanguages.split(',');
  var learnLangs = req.body.learninglanguages.split(',');
  var comms = req.body.commethod.split(',');
  var photos = []
  req.files.forEach(function(file, i){
    photos.push(req.files[i].path.replace('public/', '../'));
  });
  var updatedUser = new User(
    {
      username: req.body.username,
      firstName: req.body.fname,
      lastName: req.body.lname,
      age: req.body.age,
      gender: req.body.gender,
      spokenLanguages: spokenLangs,
      learningLanguages: learnLangs,
      info: req.body.info,
      country: req.body.country,
      city: req.body.city,
      comMethod: comms,
      photos: photos,
      lastLogin: Date.now()
    }
  );
  User.findByIdAndUpdate(req.params._id, updatedUser, function(err, user){
    if(err){
      console.log('error updating user');
      console.log(err);
    } else {
      res.redirect('/matches');
    }
  });
});

Thank you good people!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
    • ¥50 树莓派安卓APK系统签名
    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波