【发布时间】:2020-05-07 18:47:39
【问题描述】:
我已阅读此问题/答案How to call Loopback4 controller's method from another controller 但是,我的用例略有不同。
与上面的答案不同,我确实需要通过 http 访问我的 API 函数(而且我什至无法在上面的链接中得到答案才能工作)。
我的用例 我需要我的主控制器通过 iciService 获取所有位置(这一切都很好并且工作)但是我需要将所有位置数据插入另一个数据源(mongoDB),我相信通过导入我的位置控制器,对?但是我不确定到底是什么。
我对此进行了研究,但不清楚。我不是一个强大的程序员。
这是我的主控制器代码:
import { inject } from '@loopback/context';
import { iciService } from '../services/ici.service';
import { get, param } from '@loopback/rest';
export class iciController {
constructor(
@inject('services.ici')
private iciService: iciService
) {}
@get('/locations')
getall() {
return this.iciService.getLocations();
}
}
这是我需要链接到的位置控制器。
import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
repository,
Where,
} from '@loopback/repository';
import {
post,
param,
get,
getModelSchemaRef,
patch,
put,
del,
requestBody,
} from '@loopback/rest';
import {Location} from '../models';
import {LocationRepository} from '../repositories';
export class LocationController {
constructor(
@repository(LocationRepository)
public locationRepository : LocationRepository,
) {}
@post('/locations', {
responses: {
'200': {
description: 'Location model instance',
content: {'application/json': {schema: getModelSchemaRef(Location)}},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Location, {
title: 'NewLocation',
}),
},
},
})
location: Location,
): Promise<Location> {
return this.locationRepository.create(location);
}
}
【问题讨论】:
标签: loopback4