最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a Class to be used on controllers - Stack Overflow

programmeradmin1浏览0评论

I want to create a Class and make it available on my controllers. I don't want to use helpers in this particular case because I'm planning to create an npm package later with this code. I don't want to create a package now, because I don't want my code to be public.

I've tried adding this code inside a file in the hooks folder:

console.log('Hook executed!');

module.exports = class Test {
    constructor() {
        console.log('Object created!');
    }
}

When I lift the app I see that the hook it's being loaded:

info: Starting app...

Hook executed!

Then in a random controller I'm adding:

const test = new Test();

And when I execute the controller:

ReferenceError: Test is not defined

Update: According to the documentation hooks are defined in a different way. So maybe using hooks is not the best approach. Any ideas on how to make a Class available on the controllers with or without using hooks?

I want to create a Class and make it available on my controllers. I don't want to use helpers in this particular case because I'm planning to create an npm package later with this code. I don't want to create a package now, because I don't want my code to be public.

I've tried adding this code inside a file in the hooks folder:

console.log('Hook executed!');

module.exports = class Test {
    constructor() {
        console.log('Object created!');
    }
}

When I lift the app I see that the hook it's being loaded:

info: Starting app...

Hook executed!

Then in a random controller I'm adding:

const test = new Test();

And when I execute the controller:

ReferenceError: Test is not defined

Update: According to the documentation hooks are defined in a different way. So maybe using hooks is not the best approach. Any ideas on how to make a Class available on the controllers with or without using hooks?

Share Improve this question edited Sep 4, 2018 at 14:47 fsinisi90 asked Aug 31, 2018 at 16:21 fsinisi90fsinisi90 1,1581 gold badge17 silver badges49 bronze badges 4
  • Provided the Test class is defined in a file named Test.js right under the hook folder. Did you import Test from 'path_to_hooks/hooks/Test.js in your controller? – remix23 Commented Sep 3, 2018 at 16:14
  • "in a random controller": would you happen to be in this case? – Stock Overflaw Commented Sep 3, 2018 at 16:40
  • @remix23 I'm getting SyntaxError: Unexpected token import if I put that code inside a controller. – fsinisi90 Commented Sep 4, 2018 at 12:21
  • @StockOverflaw I've tried requiring the file in my bootstrap.js file and I had the same error. – fsinisi90 Commented Sep 4, 2018 at 12:28
Add a ment  | 

3 Answers 3

Reset to default 6 +50

Your files should be like these files shown bellow:

myClass.js

'use strict';
class Test {
  constructor() {
    this.name = 'test';
  }
}
module.exports = Test;

The other file which you want to use this class should look like this:

index.js

const Test = require('./myClass');
let a = new Test();
console.log(a.name);

After that when you run index.js file, you will see 'test' in your console.

Well, researching a bit more about Sails hooks I found that they have a special syntax so that's not what I wanted. Also, as I've already said, I didn't want a controller or a helper.

So, since there's no default way in Sails to create a package without actually creating it and including it to the project dependencies, I took a different approach. It's not very elegant, but it solves this particular case. I've simply placed my Test.js file inside the api folder, and included it in my controller like this:

const Test = require(__dirname + '/../../test');
let test = new Test();

The docs say that hooks are loaded in the sails.hooks object and to call a specific hook you use sails.hooks[<hook-name>]

I think you want to use something like this to use your hook sails.hooks.Test

More about it https://sailsjs./documentation/concepts/extending-sails/hooks/using-hooks

发布评论

评论列表(0)

  1. 暂无评论