Skip to content

Tutorial example has a problem to enter new item into an empty list #22120

Closed

Description

@FumioNonaka

I'm submitting a...


[x] Documentation issue or request

Current behavior

"Angular Example - Tour of Heroes: Part 6" in the tutorial of Angular 5 has a problem to enter an item into the heroines.component.

  1. Delete all items from the list of the heroines.component.
  2. Enter new item in the text field and press the [add] button.

Then listed item has neither id nor name. Browser console indicates the error message like below:

body: {error: "Collection 'heroes' id type is non-numeric or unknown. Can only generate numeric ids."}

Expected behavior

New item can be entered even if the list is empty. I recommend to revise the code as "Angular 5 Example - To be able to enter new item to empty list" shows.

Minimal reproduction of the problem with instructions

See the "Current behavior" above.

What is the motivation / use case for changing the behavior?

Confused when learning the tutorial sample.

Environment


Angular version: 5.2.4


Browser:
- [x] Chrome (desktop) version 64.0.3282.140
- [x] Firefox version 58.0.1
- [x] Safari (desktop) version 11.0.3
 
For Tooling issues:
- Platform: macOS Sierra 10.12.6

Activity

  1. trotyl commented on Feb 10, 2018

    @trotyl
    Contributor

    Nice catch, currently the in-memory-db would only auto-increase the id when there's existing id property and they're numeric:

    protected isCollectionIdNumeric<T extends { id: any }>(collection: T[], collectionName: string): boolean {
      // collectionName not used now but override might maintain collection type information
      // so that it could know the type of the `id` even when the collection is empty.
      return !!(collection && collection[0]) && typeof collection[0].id === 'number';
    }

    Suggested fix: override the genId method as we know a Hero should always have an id:

    genId(heroes: Hero[]): number {
      return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 1;
    }

    Demo here.

  2. FumioNonaka commented on Feb 10, 2018

    @FumioNonaka
    Author

    Thank you for your suggestion. I appended the solution to my article.

  3. added this to the needsTriage milestone on Feb 26, 2018
  4. avi182 commented on Mar 12, 2018

    @avi182

    How can i check which method is responsible for the id generating?

  5. modified the milestones: needsTriage, Backlog on Apr 25, 2018
  6. jenniferfell commented on Apr 25, 2018

    @jenniferfell
    Contributor

    docarea=tutorial

  7. robphoenix commented on Sep 7, 2018

    @robphoenix
    Contributor

    For anyone else with the same problem that finds this issue, the genId() method should be added into the in-memory-data.service.ts file, there's a gist here - https://gist.github.com/robphoenix/3767cf08772347e02c2434bcf3b84635

    import { InMemoryDbService } from 'angular-in-memory-web-api'
    import { Hero } from './hero'
    
    export class InMemoryDataService implements InMemoryDbService {
      createDb() {
        const heroes = [
          { id: 11, name: 'Mr. Nice' },
          { id: 12, name: 'Narco' },
          { id: 13, name: 'Bombasto' },
          { id: 14, name: 'Celeritas' },
          { id: 15, name: 'Magneta' },
          { id: 16, name: 'RubberMan' },
          { id: 17, name: 'Dynama' },
          { id: 18, name: 'Dr IQ' },
          { id: 19, name: 'Magma' },
          { id: 20, name: 'Tornado' },
        ]
        return { heroes }
      }
    
      // Overrides the genId method to ensure that a hero always has an id.
      // If the heroes array is empty,
      // the method below returns the initial number (11).
      // if heroes array is not empty, the method below returns the highest
      // hero id + 1.
      genId(heroes: Hero[]): number {
        return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11
      }
    }
  8. sreevani-ship-it commented on Oct 8, 2018

    @sreevani-ship-it
    Contributor

    Fixed as part of PR #24445

  9. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions