Getting undefined for keys when trying to extract/retrieve all the values from Firebase DB using Angular












0















I have the following piece of code that retrieves records from Firebase DB using Angular and Ionic.



It works fine but does not give me the keys for each records. It gives me 'undefined'



I looked into various online tutorials and it looks like switching from valueChanges() to snapshotChanges() will do the job, but each online tutorial shows a different way to extract records there are also version discrepancies between what I am using and the one's that are online.



Since I am coming from Java world I am quite new to JS world.



Please guide.



home.ts



export class HomePage implements OnInit {

private items$: Item = ;

constructor(private shoppingListService: ShoppingListService) {
this.shoppingListService.getShoppingList().valueChanges().subscribe((datas) => {
datas.forEach(data => {
console.log(data);

//data.key is undefined here
console.log("=>" + data.key + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: data.key, name: data.name, quantity: data.quantity, price: data.price});
})
}, (err) => {
console.log("problem:", err);
});
}

}


ShoppingListService.ts



@Injectable()
export class ShoppingListService {

//here 'shopping-list' is the name of the table
private shoppingListRef = this.db.list<Item>('shopping-list');

constructor(private db: AngularFireDatabase) {
this.getShoppingList();
}

getShoppingList() {
return this.shoppingListRef;
}

}


Item.model.ts



export interface Item {
key?: string;
name: string;
quantity: number;
price: number
}


package.json



"angularfire2": "^5.1.0",
"firebase": "^5.5.9",
"ionic-angular": "3.9.2",
"ionicons": "3.0.0",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.3.3",


ionic info



Ionic:

ionic (Ionic CLI) : 4.4.0 (C:UsersabcAppDataRoamingnpmnode_modulesionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0

System:

NodeJS : v10.13.0 (C:nodejsnode.exe)
npm : 6.4.1
OS : Windows 10


enter image description here










share|improve this question























  • if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

    – MuruGan
    Nov 26 '18 at 4:43











  • @MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

    – user2325154
    Nov 26 '18 at 5:00
















0















I have the following piece of code that retrieves records from Firebase DB using Angular and Ionic.



It works fine but does not give me the keys for each records. It gives me 'undefined'



I looked into various online tutorials and it looks like switching from valueChanges() to snapshotChanges() will do the job, but each online tutorial shows a different way to extract records there are also version discrepancies between what I am using and the one's that are online.



Since I am coming from Java world I am quite new to JS world.



Please guide.



home.ts



export class HomePage implements OnInit {

private items$: Item = ;

constructor(private shoppingListService: ShoppingListService) {
this.shoppingListService.getShoppingList().valueChanges().subscribe((datas) => {
datas.forEach(data => {
console.log(data);

//data.key is undefined here
console.log("=>" + data.key + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: data.key, name: data.name, quantity: data.quantity, price: data.price});
})
}, (err) => {
console.log("problem:", err);
});
}

}


ShoppingListService.ts



@Injectable()
export class ShoppingListService {

//here 'shopping-list' is the name of the table
private shoppingListRef = this.db.list<Item>('shopping-list');

constructor(private db: AngularFireDatabase) {
this.getShoppingList();
}

getShoppingList() {
return this.shoppingListRef;
}

}


Item.model.ts



export interface Item {
key?: string;
name: string;
quantity: number;
price: number
}


package.json



"angularfire2": "^5.1.0",
"firebase": "^5.5.9",
"ionic-angular": "3.9.2",
"ionicons": "3.0.0",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.3.3",


ionic info



Ionic:

ionic (Ionic CLI) : 4.4.0 (C:UsersabcAppDataRoamingnpmnode_modulesionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0

System:

NodeJS : v10.13.0 (C:nodejsnode.exe)
npm : 6.4.1
OS : Windows 10


enter image description here










share|improve this question























  • if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

    – MuruGan
    Nov 26 '18 at 4:43











  • @MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

    – user2325154
    Nov 26 '18 at 5:00














0












0








0








I have the following piece of code that retrieves records from Firebase DB using Angular and Ionic.



It works fine but does not give me the keys for each records. It gives me 'undefined'



I looked into various online tutorials and it looks like switching from valueChanges() to snapshotChanges() will do the job, but each online tutorial shows a different way to extract records there are also version discrepancies between what I am using and the one's that are online.



Since I am coming from Java world I am quite new to JS world.



Please guide.



home.ts



export class HomePage implements OnInit {

private items$: Item = ;

constructor(private shoppingListService: ShoppingListService) {
this.shoppingListService.getShoppingList().valueChanges().subscribe((datas) => {
datas.forEach(data => {
console.log(data);

//data.key is undefined here
console.log("=>" + data.key + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: data.key, name: data.name, quantity: data.quantity, price: data.price});
})
}, (err) => {
console.log("problem:", err);
});
}

}


ShoppingListService.ts



@Injectable()
export class ShoppingListService {

//here 'shopping-list' is the name of the table
private shoppingListRef = this.db.list<Item>('shopping-list');

constructor(private db: AngularFireDatabase) {
this.getShoppingList();
}

getShoppingList() {
return this.shoppingListRef;
}

}


Item.model.ts



export interface Item {
key?: string;
name: string;
quantity: number;
price: number
}


package.json



"angularfire2": "^5.1.0",
"firebase": "^5.5.9",
"ionic-angular": "3.9.2",
"ionicons": "3.0.0",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.3.3",


ionic info



Ionic:

ionic (Ionic CLI) : 4.4.0 (C:UsersabcAppDataRoamingnpmnode_modulesionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0

System:

NodeJS : v10.13.0 (C:nodejsnode.exe)
npm : 6.4.1
OS : Windows 10


enter image description here










share|improve this question














I have the following piece of code that retrieves records from Firebase DB using Angular and Ionic.



It works fine but does not give me the keys for each records. It gives me 'undefined'



I looked into various online tutorials and it looks like switching from valueChanges() to snapshotChanges() will do the job, but each online tutorial shows a different way to extract records there are also version discrepancies between what I am using and the one's that are online.



Since I am coming from Java world I am quite new to JS world.



Please guide.



home.ts



export class HomePage implements OnInit {

private items$: Item = ;

constructor(private shoppingListService: ShoppingListService) {
this.shoppingListService.getShoppingList().valueChanges().subscribe((datas) => {
datas.forEach(data => {
console.log(data);

//data.key is undefined here
console.log("=>" + data.key + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: data.key, name: data.name, quantity: data.quantity, price: data.price});
})
}, (err) => {
console.log("problem:", err);
});
}

}


ShoppingListService.ts



@Injectable()
export class ShoppingListService {

//here 'shopping-list' is the name of the table
private shoppingListRef = this.db.list<Item>('shopping-list');

constructor(private db: AngularFireDatabase) {
this.getShoppingList();
}

getShoppingList() {
return this.shoppingListRef;
}

}


Item.model.ts



export interface Item {
key?: string;
name: string;
quantity: number;
price: number
}


package.json



"angularfire2": "^5.1.0",
"firebase": "^5.5.9",
"ionic-angular": "3.9.2",
"ionicons": "3.0.0",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.3.3",


ionic info



Ionic:

ionic (Ionic CLI) : 4.4.0 (C:UsersabcAppDataRoamingnpmnode_modulesionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0

System:

NodeJS : v10.13.0 (C:nodejsnode.exe)
npm : 6.4.1
OS : Windows 10


enter image description here







angular typescript firebase firebase-realtime-database ionic3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 2:07









user2325154user2325154

1,77493880




1,77493880













  • if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

    – MuruGan
    Nov 26 '18 at 4:43











  • @MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

    – user2325154
    Nov 26 '18 at 5:00



















  • if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

    – MuruGan
    Nov 26 '18 at 4:43











  • @MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

    – user2325154
    Nov 26 '18 at 5:00

















if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

– MuruGan
Nov 26 '18 at 4:43





if u want the metadata then use snapshotChanges() . see github.com/angular/angularfire2/blob/master/docs/rtdb/…

– MuruGan
Nov 26 '18 at 4:43













@MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

– user2325154
Nov 26 '18 at 5:00





@MuruGan - Thanks for the answer. So how would I iterate over the values and build and array out of it? That's where majority of the confusion is for me. I have googled around for answers but all of them show different ways of doing it based on whatever version of dependency they imported. Any pointers ???

– user2325154
Nov 26 '18 at 5:00












1 Answer
1






active

oldest

votes


















0














this.shoppingListService.getShoppingList().snapshotChanges().pipe(map((actions) => {
actions.map(action => {
console.log(action.payload.doc.data());
var data = action.payload.doc.data();
var id = action.payload.doc.id;
//data.key is undefined here
console.log("=>" + id + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: id, name: data.name, quantity: data.quantity, price: data.price});
})
}), catchError((err) => {
console.log("problem:", err);
});


try it and lemme know if u have any issues. Please import pipe, map and catchError from rxjs like below



import { Observable, of, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';


Angular Version is 6



Follow this link How to use snapshotchanges






share|improve this answer


























  • As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

    – user2325154
    Nov 27 '18 at 5:35











  • wait lemme edit the code.

    – MuruGan
    Nov 27 '18 at 6:06











  • edited the code man to use pipe

    – MuruGan
    Nov 27 '18 at 7:52











  • Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

    – user2325154
    Nov 29 '18 at 2:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473923%2fgetting-undefined-for-keys-when-trying-to-extract-retrieve-all-the-values-from-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














this.shoppingListService.getShoppingList().snapshotChanges().pipe(map((actions) => {
actions.map(action => {
console.log(action.payload.doc.data());
var data = action.payload.doc.data();
var id = action.payload.doc.id;
//data.key is undefined here
console.log("=>" + id + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: id, name: data.name, quantity: data.quantity, price: data.price});
})
}), catchError((err) => {
console.log("problem:", err);
});


try it and lemme know if u have any issues. Please import pipe, map and catchError from rxjs like below



import { Observable, of, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';


Angular Version is 6



Follow this link How to use snapshotchanges






share|improve this answer


























  • As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

    – user2325154
    Nov 27 '18 at 5:35











  • wait lemme edit the code.

    – MuruGan
    Nov 27 '18 at 6:06











  • edited the code man to use pipe

    – MuruGan
    Nov 27 '18 at 7:52











  • Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

    – user2325154
    Nov 29 '18 at 2:48
















0














this.shoppingListService.getShoppingList().snapshotChanges().pipe(map((actions) => {
actions.map(action => {
console.log(action.payload.doc.data());
var data = action.payload.doc.data();
var id = action.payload.doc.id;
//data.key is undefined here
console.log("=>" + id + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: id, name: data.name, quantity: data.quantity, price: data.price});
})
}), catchError((err) => {
console.log("problem:", err);
});


try it and lemme know if u have any issues. Please import pipe, map and catchError from rxjs like below



import { Observable, of, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';


Angular Version is 6



Follow this link How to use snapshotchanges






share|improve this answer


























  • As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

    – user2325154
    Nov 27 '18 at 5:35











  • wait lemme edit the code.

    – MuruGan
    Nov 27 '18 at 6:06











  • edited the code man to use pipe

    – MuruGan
    Nov 27 '18 at 7:52











  • Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

    – user2325154
    Nov 29 '18 at 2:48














0












0








0







this.shoppingListService.getShoppingList().snapshotChanges().pipe(map((actions) => {
actions.map(action => {
console.log(action.payload.doc.data());
var data = action.payload.doc.data();
var id = action.payload.doc.id;
//data.key is undefined here
console.log("=>" + id + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: id, name: data.name, quantity: data.quantity, price: data.price});
})
}), catchError((err) => {
console.log("problem:", err);
});


try it and lemme know if u have any issues. Please import pipe, map and catchError from rxjs like below



import { Observable, of, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';


Angular Version is 6



Follow this link How to use snapshotchanges






share|improve this answer















this.shoppingListService.getShoppingList().snapshotChanges().pipe(map((actions) => {
actions.map(action => {
console.log(action.payload.doc.data());
var data = action.payload.doc.data();
var id = action.payload.doc.id;
//data.key is undefined here
console.log("=>" + id + " " + data.name + " " + data.quantity + " " + data.price);
this.items$.push({key: id, name: data.name, quantity: data.quantity, price: data.price});
})
}), catchError((err) => {
console.log("problem:", err);
});


try it and lemme know if u have any issues. Please import pipe, map and catchError from rxjs like below



import { Observable, of, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';


Angular Version is 6



Follow this link How to use snapshotchanges







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 7:46

























answered Nov 26 '18 at 5:37









MuruGanMuruGan

675417




675417













  • As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

    – user2325154
    Nov 27 '18 at 5:35











  • wait lemme edit the code.

    – MuruGan
    Nov 27 '18 at 6:06











  • edited the code man to use pipe

    – MuruGan
    Nov 27 '18 at 7:52











  • Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

    – user2325154
    Nov 29 '18 at 2:48



















  • As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

    – user2325154
    Nov 27 '18 at 5:35











  • wait lemme edit the code.

    – MuruGan
    Nov 27 '18 at 6:06











  • edited the code man to use pipe

    – MuruGan
    Nov 27 '18 at 7:52











  • Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

    – user2325154
    Nov 29 '18 at 2:48

















As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

– user2325154
Nov 27 '18 at 5:35





As soon as I copy-paste the above code it complains at the very first line. Property 'map' does not exist on type 'Observable<AngularFireAction<DatabaseSnapshot<Item>>>'.

– user2325154
Nov 27 '18 at 5:35













wait lemme edit the code.

– MuruGan
Nov 27 '18 at 6:06





wait lemme edit the code.

– MuruGan
Nov 27 '18 at 6:06













edited the code man to use pipe

– MuruGan
Nov 27 '18 at 7:52





edited the code man to use pipe

– MuruGan
Nov 27 '18 at 7:52













Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

– user2325154
Nov 29 '18 at 2:48





Special thanks to you for the extra effort you are putting in but even after copy-pasting the above changes it was complaining about the import statements that you mentioned above. To me it looks like a RxJS version mismatch issue. The editor is complaining about the doc and .data() also. However, when I change it to .val() the editor does not show any red marks and everything gets compiled but still no data shows up in the UI. When I look at the console logs it does not anything in action.payload.val(). So it looks like it is not even going inside the actions.map callback method.

– user2325154
Nov 29 '18 at 2:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473923%2fgetting-undefined-for-keys-when-trying-to-extract-retrieve-all-the-values-from-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin