Reputation: 45
I am trying to create a service in sailsjs so I can access some business logic from within multiple controllers, I've stumbled upon a number of issues... The service is an 'Action log' I hope to log data about specific api calls to the database via a model.
The file structure I have:
api/services/actionService.js - the service to receive data from controller and log to model
api/models/Action.js - functioning model to store logged actions
api/controllers/ActionController.js - functioning controller to respond to get requests for the action model
api/controllers/AnotherController.js - I wish to access the actionService from here to input data
I am unsure how to reference the service from AnotherController.js; according to the sails docs I should be able to call ActionService(data)
but I get an ActionService is not defined
error. Is there something I need to do to load this dependency in the controller?
Am I able to reference a model from within the service? At the moment I have something like Action.create(action)...
in the service. This hasn't caused problems yet as I haven't got passed the first issue but wondering whether I also need to load this as a dependency in the service?
I'm new to implementing services in sailsjs, any help is appreciated.
Upvotes: 1
Views: 1196
Reputation: 9392
You can access your services in any controller using this syntax:
<yourServicefilename>.method();
In your case it would be actionService.<yourMethodName>()
.
Upvotes: 3