gpt4 book ai didi

javascript - SailsJS 从 Controller 访问服务

转载 作者:行者123 更新时间:2023-11-30 12:40:42 25 4
gpt4 key购买 nike

我正在尝试在 sailsjs 中创建一个服务,以便我可以从多个 Controller 中访问一些业务逻辑,我偶然发现了一些问题......该服务是一个“操作日志”,我希望记录有关的数据通过模型对数据库进行特定的 API 调用。

我的文件结构:

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
  1. 我不确定如何从 AnotherController.js 引用服务;根据sails docs我应该能够调用 ActionService(data) 但我收到了 ActionService is not defined 错误。我需要做些什么才能在 Controller 中加载此依赖项吗?

  2. 我可以从服务中引用模型吗?目前我在服务中有类似 Action.create(action)... 的东西。这还没有引起问题,因为我还没有通过第一个问题,但想知道我是否还需要将它作为服务的依赖项加载?

我是在 sailsjs 中实现服务的新手,感谢任何帮助。

最佳答案

您可以使用以下语法在任何 Controller 中访问您的服务:

<yourServicefilename>.method();

在您的情况下,它将是 actionService.<yourMethodName>() .

关于javascript - SailsJS 从 Controller 访问服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24565022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com