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

时间:2021-03-31 17:08:31

I am creating a resourceful api using Nodejs. I have created a UserController class which will contain the usual methods. I am trying to assign some variable in my constructor class to use in those methods.

我正在使用Nodejs创建一个足智多谋的api。我创建了一个UserController类,它将包含常用方法。我试图在我的构造函数类中分配一些变量以在这些方法中使用。

The variable may get changed to some repo class obj at a later date. So i want to know how i can use a variable i set in my constructor in my index function.

该变量可能会在以后更改为某个repo类obj。所以我想知道如何在我的索引函数中使用我在构造函数中设置的变量。

nodejs : v4.5.0

nodejs:v4.5.0

When i run navigate to /users/ which calls the index function i get this.

当我运行导航到/ users /调用索引函数时,我得到了这个。

```

TypeError: Cannot read property 'name' of undefined

```

Route

```

var express    = require('express');
var router     = express.Router();
var controller = require('../controllers/user.controller');
    controller = new controller();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.store);
router.put('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

```

controller ```

'use strict';

var User  = require('../models/user.model');

class UserController {

    constructor() {
        this.name = "NameHere";
    }

    index(req, res) {

        console.log("My Name Is: " + this.name);

    }

    show(req, res) {


    }

    update(req, res) {

    }

    store(req, res) {

    }

    destroy(req, res) {

    }

}

module.exports = UserController;

```

1 个解决方案

#1


0  

You would write your routers like that:

你会像这样写你的路由器:

controller

'use strict';

var data = {
  name :  'NameHere'
};


module.exports = {
  index(req, res) {
    console.log("My Name Is: " + data.name);
  },

  show(req, res) {

  },

  update(req, res) {

  },

  store(req, res) {

  },

  destroy(req, res) {

  }
};

Route

var controller = require('../controllers/user.controller');

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.store);
router.put('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

If you really need to use different instance of your controller or if you need to pass some config options to it then you would do something like that:

如果你真的需要使用你的控制器的不同实例,或者你需要传递一些配置选项,那么你会做这样的事情:

controller

'use strict';

module.exports = function( options ) {
  var data = {
    name :  options.name
  };


  return {
    index(req, res) {
      console.log("My Name Is: " + data.name);
    },

    show(req, res) {

    },

    update(req, res) {

    },

    store(req, res) {

    },

    destroy(req, res) {

    }
  };
}

Route

var controller = require('../controllers/user.controller')({
    name : 'some name'
});

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.store);
router.put('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

#1


0  

You would write your routers like that:

你会像这样写你的路由器:

controller

'use strict';

var data = {
  name :  'NameHere'
};


module.exports = {
  index(req, res) {
    console.log("My Name Is: " + data.name);
  },

  show(req, res) {

  },

  update(req, res) {

  },

  store(req, res) {

  },

  destroy(req, res) {

  }
};

Route

var controller = require('../controllers/user.controller');

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.store);
router.put('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

If you really need to use different instance of your controller or if you need to pass some config options to it then you would do something like that:

如果你真的需要使用你的控制器的不同实例,或者你需要传递一些配置选项,那么你会做这样的事情:

controller

'use strict';

module.exports = function( options ) {
  var data = {
    name :  options.name
  };


  return {
    index(req, res) {
      console.log("My Name Is: " + data.name);
    },

    show(req, res) {

    },

    update(req, res) {

    },

    store(req, res) {

    },

    destroy(req, res) {

    }
  };
}

Route

var controller = require('../controllers/user.controller')({
    name : 'some name'
});

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.store);
router.put('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;