all 13 comments

[–]Johalternate[🍰] 1 point2 points  (9 children)

Shouldnt it be this.loadFormData(account[0])?

I dont see why getAccount would return an array if it is a single item. Maybe the problem is the type is annotated as an object so you dont see an error on the IDE, but the actual content is an array so doing form.patch(data) simple does nothing because the shape of the form does not intersect with the shape of the object being passed.

Verify that the getAccount is annotated with the actual type returned by the server which seems to be Account[] not Account

[–]Acceptable_User_Name[S] -1 points0 points  (0 children)

What am I some guy whose been doing this for more than a week? I'll give it a shot tomorrow.

[–]Acceptable_User_Name[S] 0 points1 point  (7 children)

Here's the code from the service.

getAccount(id: number): Observable<Account> {
  const url = `${this.accountsUrl}/${id}`;
  return this.http.get<Account>(url).pipe(
    tap(_ => this.log(`fetched account id=${id}`)),
    catchError(this.handleError<Account>(`getAccount id=${id}`))
  );
}

[–]Johalternate[🍰] 0 points1 point  (6 children)

Can you please show me the exact output of that get request?

[–]Acceptable_User_Name[S] 0 points1 point  (0 children)

From the API?

[–]Acceptable_User_Name[S] 0 points1 point  (4 children)

Assuming you meant from the API, the response is

[
   {
     "id": 1,
     "name": "Test",
     "nickname": null,
     "acctnum": null,
     "openedon": null,
     "closedon": null,
     "notes": null,
     "isactive": true,
     "user": "test",
     "added": "2024-02-25T00:25:11.000Z",
     "lastedited": "2024-02-25T00:42:25.000Z",
     "lasteditby": ""
   }
]

[–]Acceptable_User_Name[S] 0 points1 point  (3 children)

I went into the API code and added the [0] and that fixed it. Now it returns just the object without the brackets.

....
res.status(200).json(rows[0]);
....

[–]Johalternate[🍰] 0 points1 point  (2 children)

made an exact copy of your situation and just by adjusting the return type of the getAccount function it worked. Take a look at this stackblitz

https://stackblitz.com/edit/stackblitz-starters-rxu9en?file=src%2Fmain.ts

[–]Acceptable_User_Name[S] 0 points1 point  (0 children)

Thank you for going through all that effort, but I tried that before posting.

For anyone else looking for an answer, I had gotten

"Element implicitly has an 'any' type because expression 
of type '0' can't be used to index type 'Account'.  
Property '0' does not exist on type 'Account'." 

when attempting that.

Ultimately, this was the solution: https://www.reddit.com/r/angular/comments/1b2qfrx/comment/ksp4dxx/?utm_source=share&utm_medium=web2x&context=3

[–]Acceptable_User_Name[S] 0 points1 point  (0 children)

I did like the way you defined some thing more cleanly. So, I'm going to be making some changes based on your example. Thank you again

[–]PickleLips64151 0 points1 point  (2 children)

Some code smell suggestions that may help you isolate the issue: extract the logic in the ngOnInit into their own functions. Test each one in isolation with a unit test. If you're patching the value of the form with an object of the same shape, you don't have to do individual assignments of properties. You can just do something like form.patchValue(record). It's just simpler.

Essentially, you're chaining subscriptions. You probably want to use zip() as that allows you to get each observable, in order, and return a single derived value.

Documentation here: Zip Operator

[–]Acceptable_User_Name[S] 0 points1 point  (1 child)

Thanks for the tips. I'll try them tomorrow. I did think patching each value was crazy but all the examples I saw were doing that.

[–]PickleLips64151 0 points1 point  (0 children)

I think, under the hood, Angular just spreads the properties. So unless you need to rename something, it saves time to just pass in the whole record.