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 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器