0
votes

I've read this question/answer How to call Loopback4 controller's method from another controller HOWEVER, my use case is slightly different.

Unlike that answer above, I DO need my API functions to be accessible via http (and I also couldn't get even get the answer in the link above to work).

My Use Case I need for my Main Controller to go and get all Locations via the iciService (which is all good and working) BUT then I need to insert all of that Locations data into another datasource (mongoDB), I believe by importing my Locations Controller, right? But how exactly is what I'm not sure on.

I've research this but I'm unclear. I'm not a strong programmer.

Here's my Main Controller code:

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();
  }
}

And here's the Locations controller that I need to link out to.

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);
  }
}
1

1 Answers

0
votes

Ok after much research on Github repos of other Loopback4 apps, I figured out to get the other Stackoverflow to work (How to call Loopback4 controller's method from another controller).

Although my fix doesn't technically call another Controller, I am able to access the Repository that the Controller (Locations) uses.

So, I just had to add the import for the @repository at the top of my Main Controller, like so:

import { repository } from '@loopback/repository';

So my Main Controller page looks like:

import { inject } from '@loopback/context';
import { repository } from '@loopback/repository';
import { iciService } from '../services/ici.service';
import { get, param } from '@loopback/rest';

export class iciController {
  constructor(
    @inject('services.ici')
    private iciService: iciService,

    @repository(JobRepository)
    private jobRepository: JobRepository,
  ) {}

  @get('/locations')
  getall() {

    //NOW YOU CAN ACCESS THE FUNCTIONS IN THE jobRepository
    return this.jobRepository.count();
  }
}