AngularJS:无法访问和修改用户信息(PUT,GET)

时间:2016-08-09 07:27:00

标签: javascript angularjs xmlhttprequest components angular-fullstack

我正在构建一个基于角度1.5组件的应用程序(使用angular-fullstack),并希望修改我的用户信息。

密码更改功能已内置到angular-fullstack中,我尝试使用相同的方法进行用户名修改,但我无法使其工作。 我的javascript控制台或普通控制台也没有任何错误。

由于我有很多文件,很难做一个jsfiddle(我试过......),所以这里是我的代码细分:

UserPageConfigure.html(只有有趣的部分)

 <form class="form" name="form2" ng-submit="$ctrl.changeName(form2)" novalidate>

        <div class="form-group">
          <label>Current Name</label>

          <input type="name" name="name" class="form-control" ng-model="$ctrl.user.oldName"
                 mongoose-error/>
          <p class="help-block" ng-show="form2.name.$error.mongoose">
              {{ $ctrl.errors.other }}
          </p>
        </div>

        <div class="form-group">
          <label>New name</label>

          <input type="name" name="newName" class="form-control" ng-model="$ctrl.user.newName"
                 ng-minlength="3"
                 required/>
          <p class="help-block"
             ng-show="(form2.newName.$error.minlength || form.newName.$error.required) && (form2.newName.$dirty || $ctrl.submitted)">
            name must be at least 3 characters.
          </p>
        </div>

        <div class="form-group">
          <label>Confirm New name</label>

          <input type="name" name="confirmName" class="form-control" ng-model="$ctrl.user.confirmName"
                 match="$ctrl.user.newName"
                 ng-minlength="3"
                 required=""/>
          <p class="help-block"
             ng-show="form2.confirmName.$error.match && $ctrl.submitted">
            names must match.
          </p>

        </div>

        <p class="help-block"> {{ $ctrl.message }} </p>

        <button class="btn btn-lg btn-primary" type="submit">Save changes</button>
      </form>

这是与之关联的Js文件

(function() {
  class userPageConfigureController {
    constructor(
      $scope, $element, $attrs,$http, $uibModal, $timeout, Auth, User)
    {

this.Auth=Auth;
this.getCurrentUser = Auth.getCurrentUser;
changeName(form2) {
this.submitted = true;

if (form2.$valid) {
  this.Auth.changeName(this.user.oldName, this.user.newName)

    .then(() => {
      console.log(this.user.oldName);
      console.log(this.user.newName);
      this.message = 'Name successfully changed.';
    })
    .catch(() => {
      form2.name.$setValidity('mongoose', false);
      this.errors.other = 'Incorrect name';
      this.message = '';
    });
    }
  }
}
angular.module('myApp').component('userPageConfigure', {
  templateUrl: 'components/appcomponent/userPage/userPageConfigure/userPageConfigure.html',
  controller: userPageConfigureController,
  bingings:{
    userstatus:'='
  }
});
})();

我知道这很难消化,但是等等!它变得更好,这是我的user.controller.js文件的内容

export function changeName(req, res, next) {

  var userName = req.user.name;
  var oldname = String(req.body.oldName);
  var newName = String(req.body.newName);

  return User.find({
    where: {
      name: userName
    }
  })
    .then(user => {
      if (user.authenticate(oldName)) {
        user.name = newName;
        return user.save()
          .then(() => {
            res.status(204).end();
          })
          .catch(validationError(res));
      } else {
        return res.status(403).end();
      }
    });
}

最后,这是我在auth.service.js中的函数

function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
var safeCb = Util.safeCb;
var currentUser = {};

var Auth = {

----------

changePassword(oldPassword, newPassword, callback) {
    return User.changePassword({
        id: currentUser._id
      }, {
        oldPassword: oldPassword,
        newPassword: newPassword
      }, function() {
        return safeCb(callback)(null);
      }, function(err) {
        return safeCb(callback)(err);
      })
      .$promise;
  },
return Auth;
}

  angular.module('merciPublicApp.auth')
    .factory('Auth', AuthService);
})();

如果你一直在读这里......谢谢你!如果您有任何见解......再次感谢您!

奖金:

这是我的index.js(服务器端)

import {Router} from 'express';
import * as controller from './user.controller';
import * as auth from '../../auth/auth.service';

var router = new Router();

router.get('/', auth.hasRole('admin'), controller.index);
router.delete('/:id', auth.hasRole('admin'), controller.destroy);
router.get('/me', auth.isAuthenticated(), controller.me);
router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
// The one i'm trying to do router.put('/:id/name', auth.isAuthenticated(), controller.changeName);
router.get('/:id', auth.isAuthenticated(), controller.show);
router.post('/', controller.create);

module.exports = router;

0 个答案:

没有答案