0

Here the user.js file with class and exports

class User {

        static async select() {
          const selectResult = await usersDAL.listUsers();
          return camelizeKeys(selectResult);
      }
    }

module.exports.User = User;

I want to use in usersApi file

router.get('/',(req, res) =>{

    let userList = new  User().select();
    res.send(userList);

});

error show

(intermediate value).select is not a function

Please help me

Md. Maidul Islam
  • 564
  • 6
  • 10

3 Answers3

1

※ At first you should use newer Node.js version 6.0 if you can.

you can export objects by module.exports and import by require.

how to export

class User {

    static async select() {
      const selectResult = await usersDAL.listUsers();
      return camelizeKeys(selectResult);
  }
}

module.exports = User;

how to import

const User = require('../model/user') // this path is dummy.
router.get('/', async (req, res) =>{

    let userList = await User.select();
    res.send(userList);
});

User.select() is a method to be used as async method. You should wrap Promise-block or use async-await statements.

Sorry my English and I don't execute the code above. It mightn't work well.

t.kuriyama
  • 167
  • 5
  • your opinion checked but got same error => (intermediate value).select is not a function – Md. Maidul Islam Jan 30 '19 at 16:06
  • 1
    @Md.MaidulIslam if you have how to access to the static method, you can check this link below out. https://stackoverflow.com/questions/43614131/js-call-static-method-from-class/43614217 – t.kuriyama Jan 30 '19 at 16:09
0

By using require like so:

const User = require('./user.js').User

The ./user.js path is assuming the user.js file is in the same directory as usersApi.

That being said you probably want to only export the User:

user.js

// ... rest of file

module.exports = User;

usersApi.js

const User = require('./user.js')
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
0

According to your question and class diagram of user.js file

class User {

        static async select() {
          const selectResult = await usersDAL.listUsers();
          return camelizeKeys(selectResult);
      }
    }

module.exports.User = User;

you want to export and use it in usersApi file. In this way, you can use

const User = require('.user');

router.get('/', async (req, res) =>{

    let userList = await User.User.select();
    res.send(userList);
});

Because you export User object in user.js file (ex: module.exports.User = User) that's way, you need User.User.select(). Another information you should know that static async method should be call inside async or promise method. For this reason I use await User.User.select()

Md. Maidul Islam
  • 564
  • 6
  • 10