question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
trotylcommented, Feb 10, 2018

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.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 14, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Empty List Tutorial – How to Create ... - freeCodeCamp
In the example below, we create an empty list and assign it to the variable num . Then, using a for loop, we...
Read more >
Angular 5 Example - To be able to enter new item to empty list
When the array of data comes to be empty, nothing has the id property. Then number for id does not seem to be...
Read more >
How to Create an Empty List in Python? - Finxter
To create an empty list in Python, you can use two ways. First, the empty square bracket notation [] creates a new list...
Read more >
Python Empty List | How to Declare Empty List with Examples
In this article, an empty list is created by just declaring square brackets [] with no elements within the assignment statement's brackets. It...
Read more >
Declare an empty List in Python - GeeksforGeeks
However, Have you ever wondered about how to declare an empty list in Python? This can be achieved by two ways i.e. either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found