0

I'm learning Firebase and Angular4 and am using AngularFire2.

I have a list of items with a custom key and I have a list of owners, also with a custom key in a firebase db.

I first joined the item with his owner by using its key in the item and when retrieving the item I would do a second request to get the data of the owner.

I understand that this is not the way to handle this in a NoSQL database, it is better to save the whole owner object as a child in my item. Then I would not need to get the owner detail separately.

I have a form to edit the item data, which has a select box with all the owners. This list of owners is retrieved in ngOnInit. The selectbox is populated as:

<select class="form-control" formControlName="owner" id="owner">
  <option *ngFor="let ownerList of owners" [value]="ownerList">
    {{ownerList.name}}
   </option>
</select>

I'm not sure if I create this select box correctly, but it seems to be working.

OnSubmit I do:

updateItem(alias: string, formValues: Item) {
  const mergedUpdate = {};
  mergedUpdate[ "item/" + alias] = formValues;
  this.af.database.ref().update(mergedUpdate);
}

When I now look at how the data is saved in the firebase console I see for owner [object Object]

I've been reading numerous posts but can't figure out what I'm doing wrong. Please advice.

Community
  • 1
  • 1
Paul Meems
  • 3,002
  • 4
  • 35
  • 66
  • What is `Item`? Maybe you need to parse to plain JSON before saving. – Mariano Córdoba Sep 08 '17 at 13:25
  • `Item` is an object. What's the problem? – camden_kid Sep 08 '17 at 13:27
  • You are not making object in correct way. as @MarianoCórdoba mentioned prepare a JSON object e.r `{'property1':'value1',..................}` – Zaid Mirza Sep 08 '17 at 14:17
  • I found the problem using https://scotch.io/tutorials/how-to-deal-with-different-form-controls-in-angular-2. I need to use `ngValue` instead of `value`. Now it is working as expected. If one of you post this, I'll mark it as an answer. – Paul Meems Sep 08 '17 at 14:23

0 Answers0