TypeError:无法读取未定义的属性'id'-Sails和MongoDB

麦克尼

Sails @ beta,mongodb,Windows 7 32位,

我试图通过该应用创建新用户后收到上述错误。当我从config / policies.js中删除策略isPawn时,一切正常。我不明白这项政策有什么问题。在我看来,我错误地访问了mongodb id属性。

isPawn.js https://github.com/mrcn/C_Express/blob/master/api/policies/isPawn.js

module.exports = function(req, res, next) {

  var sessionUserMatchesId = req.session.User.id === req.param('id');
  var isAdmin = req.session.User.admin;

  // The requested id does not match the user's id,
  // and this is not an admin

  if (!(sessionUserMatchesId || isAdmin)) {
    var noRightsError =
    [{
      name: 'noRights',
      message: 'You must be an admin.'
    }];
    req.session.flash = {
      err: noRightsError
    };
    res.redirect('/session/new');
    return;
  }

  next();

};

用户模型-user.js https://github.com/mrcn/C_Express/blob/master/api/models/User.js

module.exports = {

  schema: true,

  attributes: {

    name: {
      type: 'string',
      required: true
    },

    email: {
      type: 'string',
      email: true,
      required: true,
      unique: true
    },

    admin: {
      type: 'boolean',
      defaultsTo: false
    },

    encryptedPassword: {
      type: 'string'
    },

    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      delete obj.confirmation;
      delete obj.encryptedPassword;
      delete obj._csrf;
      return obj;
    }
  },

  beforeCreate: function (values, next) {
      //this checks to make sure the passwords match
    if (!values.password || values.password != values.confirmation) {
      return next({err: ["Password\'s don\'t match"]});
    }

    require('bcrypt').hash(values.password, 10, function passwordEncrypt(err, encryptedPassword){
      if (err) return next(err);
      values.encryptedPassword = encryptedPassword;

      console.log();
      console.log(values);
      console.log();

      // values.online=true;
      next();
    });
  }
};

UserController.js:https//github.com/mrcn/C_Express/blob/master/api/controllers/UserController.js

module.exports = {

  // this loads the signup page --> new.ejs
  new : function (req, res) {
    res.locals.flash = _.clone(req.session.flash);
    res.view();
    req.session.flash = {};
  },

  create: function (req, res, next) {

    // Create a user with the params sent from
    // the signup form --> new.ejs
    User.create(req.params.all(), function userCreate (err, user) {

      if (err) {
        console.log(err);
        req.session.flash = {
          err: err
        }

        // if error redirect back to signup page
        return res.redirect('/user/new');
      }

      // after successfully create the user
      // redirect to the show action
      // res.json(user);
      // req.session.flash = {};

      res.redirect('/user/show/'+user.id);
    });
  },

    show: function (req, res, next) {
      User.findOne (req.param('id'), function foundUser (err,user) {
        if (err) return next(err);
        if (!user) return next();
        res.view({
          user: user
        });
      });
    },

    index: function (req, res, next) {
      // EP 13
      // console.log(new Date());
      // console.log(req.session.authenticated);

      //get an array of all users in the User Collection (table)
      User.find(function foundUsers (err, users) {
        if (err) return next(err);
        // pass the array down to the /views/index.ejs page
        // we're rendering our index.ejs passing our users as an object.
        res.view({
          users: users
        });
      });
    },

    edit: function (req, res, next) {
      //Find the user from the id passed in via params
      User.findOne(req.param('id'), function foundUser (err, user){
        if (err) return next(err);
        if (!user) return next('User doesn\nt exist!');

        res.view({
          user: user
        });
      });
    },

    update: function (req, res, next) {
      User.update(req.param('id'), req.params.all(), function userUpdated (err) {
        if (err) {
          return res.redirect('/user/edit/' + req.param('id'));
        }

        res.redirect('/user/show/' + req.param('id'));
        });
    },

    destroy: function (req, res, next) {
      User.findOne(req.param('id'), function foundUser (err, user) {
        if (err) return next(err);
        if (!user) return next('User doesn\nt exist!');

        User.destroy(req.param('id'), function userDestroyed(err) {
          if (err) return next(err);
        });
        res.redirect('/user');
      });
    }
};
第454章

似乎您正在尝试访问的id属性req.session.User,但req.session.User至少在某些情况下不存在。如果总是存在,那么我将检查负责设置它的代码。否则,将策略的两行更改为:

var sessionUserMatchesId = (req.session.User && req.session.User.id === req.param('id'));
var isAdmin = req.session.User && req.session.User.admin;

如果req.session.User未定义,将防止崩溃

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

来自分类Dev

Gatsby TypeError-无法读取未定义的属性“ id”

来自分类Dev

MERN-TypeError:无法读取未定义的属性“ id”

来自分类Dev

Angular Material TypeError:无法读取未定义的属性“ id”

来自分类Dev

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

来自分类Dev

JWT-TypeError:无法读取未定义的属性“id”

来自分类Dev

继承和“ TypeError:无法读取未定义的属性'someProperty'”

来自分类Dev

无法读取未定义的属性“ id”

来自分类Dev

Angular2-TypeError:无法读取(Typescript)中未定义的属性'Id'

来自分类Dev

AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性'__id'

来自分类Dev

未捕获的TypeError:无法读取registerNgModuleType上未定义的属性“ id”-角度PWA

来自分类Dev

NodeJs TypeError:无法读取未定义的属性“ Customer_id”

来自分类Dev

core.js:6406错误TypeError:无法读取未定义的属性“ id”

来自分类Dev

TypeError:无法读取未定义的属性“ id” Discord.js

来自分类Dev

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ id”

来自分类Dev

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ id”

来自分类Dev

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ public_id”

来自分类Dev

core.js:30235未捕获的TypeError:无法读取未定义的属性'id'

来自分类Dev

AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性“ __id”

来自分类Dev

未捕获的TypeError:无法读取未定义的属性'company_id'

来自分类Dev

MongoError:TypeError:使用$ or时,无法读取未定义的属性“ id”

来自分类Dev

未捕获的TypeError:无法读取未定义的sails.io.js的属性“ prototype”

来自分类Dev

灰烬路线:无法读取未定义的属性“ id”

来自分类Dev

guildBanAdd无法读取未定义的属性“ id”

来自分类Dev

删除时出错:无法读取未定义的属性“ id”

来自分类Dev

Serverless:“ errorMessage”:“无法读取未定义的属性” id“,

来自分类Dev

用户注销后无法读取未定义的属性“ _id”

来自分类Dev

guildBanAdd无法读取未定义的属性“ id”

来自分类Dev

无法读取未定义的属性“ id”。表示

Related 相关文章

  1. 1

    UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

  2. 2

    Gatsby TypeError-无法读取未定义的属性“ id”

  3. 3

    MERN-TypeError:无法读取未定义的属性“ id”

  4. 4

    Angular Material TypeError:无法读取未定义的属性“ id”

  5. 5

    UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

  6. 6

    JWT-TypeError:无法读取未定义的属性“id”

  7. 7

    继承和“ TypeError:无法读取未定义的属性'someProperty'”

  8. 8

    无法读取未定义的属性“ id”

  9. 9

    Angular2-TypeError:无法读取(Typescript)中未定义的属性'Id'

  10. 10

    AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性'__id'

  11. 11

    未捕获的TypeError:无法读取registerNgModuleType上未定义的属性“ id”-角度PWA

  12. 12

    NodeJs TypeError:无法读取未定义的属性“ Customer_id”

  13. 13

    core.js:6406错误TypeError:无法读取未定义的属性“ id”

  14. 14

    TypeError:无法读取未定义的属性“ id” Discord.js

  15. 15

    错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ id”

  16. 16

    错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ id”

  17. 17

    UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ public_id”

  18. 18

    core.js:30235未捕获的TypeError:无法读取未定义的属性'id'

  19. 19

    AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性“ __id”

  20. 20

    未捕获的TypeError:无法读取未定义的属性'company_id'

  21. 21

    MongoError:TypeError:使用$ or时,无法读取未定义的属性“ id”

  22. 22

    未捕获的TypeError:无法读取未定义的sails.io.js的属性“ prototype”

  23. 23

    灰烬路线:无法读取未定义的属性“ id”

  24. 24

    guildBanAdd无法读取未定义的属性“ id”

  25. 25

    删除时出错:无法读取未定义的属性“ id”

  26. 26

    Serverless:“ errorMessage”:“无法读取未定义的属性” id“,

  27. 27

    用户注销后无法读取未定义的属性“ _id”

  28. 28

    guildBanAdd无法读取未定义的属性“ id”

  29. 29

    无法读取未定义的属性“ id”。表示

热门标签

归档