admin管理员组文章数量:1322838
I have a loopback 4 controller with a function that I don't want to expose via HTTP. I would like to be able to call the function from another controller.
How can I do this? Is there any way of injecting a controller in another controller? (I 'm able to inject repositories in controllers, but not controllers in other controllers).
I have a loopback 4 controller with a function that I don't want to expose via HTTP. I would like to be able to call the function from another controller.
How can I do this? Is there any way of injecting a controller in another controller? (I 'm able to inject repositories in controllers, but not controllers in other controllers).
Share Improve this question edited Jun 1, 2019 at 18:35 nassim 1,5531 gold badge15 silver badges28 bronze badges asked Jun 1, 2019 at 17:08 DinisDinis 511 silver badge5 bronze badges3 Answers
Reset to default 5You have to first import repository of another controller e.g.
import { MemberRepository, EmailTemplateRepository } from '../repositories';
then you have to inject it in constructor like this:-
@repository(EmailTemplateRepository) public emailTemplateRepository: EmailTemplateRepository,
then after you can use any function of controller like this:-
const template = await this.emailTemplateRepository.findOne({
where: {
slug: 'user-password-reset',
status: 1
}
});
Answer is here: https://github./strongloop/loopback-next/issues/3028
@inject(‘controllers.AnotherController’) c: AnotherController
Ok I figured out how to make this work. You have to import the @repository ponent where the rest of the other import statements are, like so:
import { repository } from '@loopback/repository';
Adding this, will allow for, @repository(EmailTemplateRepository) public emailTemplateRepository: EmailTemplateRepository, to work.
本文标签: javascriptHow to call Loopback4 controller39s method from another controllerStack Overflow
版权声明:本文标题:javascript - How to call Loopback4 controller's method from another controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742115818a2421463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论