Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

todo tutorial, add correct code to src/controllers/todo.controller.ts #3977

Closed
Fr4nZ82 opened this issue Oct 21, 2019 · 1 comment · Fixed by #3991
Closed

todo tutorial, add correct code to src/controllers/todo.controller.ts #3977

Fr4nZ82 opened this issue Oct 21, 2019 · 1 comment · Fixed by #3991
Labels

Comments

@Fr4nZ82
Copy link

Fr4nZ82 commented Oct 21, 2019

Steps to reproduce

follow the todo tutorial and the "Bonus: Integrate with a geo-coding service"

Current Behavior

at the end of "Bonus: Integrate with a geo-coding service" there is an incorrect snippet:

export class TodoController {
  // constructor, etc.
  @post('/todos')
  async createTodo(@requestBody() todo: Todo) {
    if (!todo.title) {
      throw new HttpErrors.BadRequest('title is required');
    }
    if (todo.remindAtAddress) {
      // TODO handle "address not found"
      const geo = await this.geoService.geocode(todo.remindAtAddress);
      // Encode the coordinates as "lat,lng"
      todo.remindAtGeo = `${geo[0].y},${geo[0].x}`;
    }
    return this.todoRepo.create(todo);
  }
  // other endpoints remain unchanged
}

https://loopback.io/doc/en/lb4/todo-tutorial-geocoding-service.html

Expected Behavior

the correct one must be:

@post('/todos', {
    responses: {
      '200': {
        description: 'Todo model instance',
        content: {'application/json': {schema: getModelSchemaRef(Todo)}},
      },
    },
  })
  async create(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Todo, {
            title: 'NewTodo',
            exclude: ['id'],
          }),
        },
      },
    })
    todo: Omit<Todo, 'id'>,
  ): Promise<Todo> {
    if (todo.remindAtAddress) {
      // TODO(bajtos) handle "address not found"
      const geo = await this.geoService.geocode(todo.remindAtAddress);
      // Encode the coordinates as "lat,lng" (Google Maps API format). See also
      // https://stackoverflow.com/q/7309121/69868
      // https://gis.stackexchange.com/q/7379
      // eslint-disable-next-line require-atomic-updates
      todo.remindAtGeo = `${geo[0].y},${geo[0].x}`;
    }
    return this.todoRepository.create(todo);
  }
@Fr4nZ82 Fr4nZ82 added the bug label Oct 21, 2019
@Fr4nZ82
Copy link
Author

Fr4nZ82 commented Oct 21, 2019

is "// eslint-disable-next-line require-atomic-updates" necessary? No better solution?

irineul added a commit to irineul/loopback-next that referenced this issue Oct 22, 2019
Fixing the code with the correct one (createTodo w/ geocode).
Fixes loopbackio#3977
raymondfeng pushed a commit that referenced this issue Oct 23, 2019
Fixing the code with the correct one (createTodo w/ geocode).
Fixes #3977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant