Im trying to make a appliation that finds quizzes on kahhot but when im running the code the data is getting undefined even tho 2 lines before I get this data
- First console.log: card: { type: 'quiz', title: 'Guess Who', description: 'Start of term quiz to see how well the class know each other', slug: 'guess-who', cover: 'https://media.kahoot.it/e6740327-876d-4610-9fba-bc305f4bcae0_opt', coverMetadata: [Object], draftExists: false, number_of_questions: 38, creator: 'bfdca93b-efba-4ccb-a64d-c5538b24ba94', creator_username: 's_swan', creatorPrimaryUsageType: 'SCHOOL', creator_avatar: {}, badges: [], visibility: 1, locked: false, writeProtection: false, featured: false, young_featured: false, sponsored: false, draft: false, combined: false, compatibility_level: 6, sample_questions: [Array], number_of_plays: 110, number_of_players: 274, total_favourites: 0, question_types: [Array], created: 1471873436135, modified: 1474121015925, access: [Object], duplication_disabled: false, uuid: 'b58c19b5-b098-4531-a1c6-e8e735e8f7b2' }
- Second console.log(): undefined
In the "for" loop the data are going to be undefined, how?
I have this code:
var options = {
'method': 'GET',
'url': 'https://create.kahoot.it/rest/kahoots/?query=' nam '&cursor=0&limit=20&topics&grades=&orderBy=relevance&searchCluster=1&includeExtendedCounters=False',
'headers': {
'Authorization': 'Bearer ' token
}
};
// Starting the request
request(options, function (error, response, body) {
if (error) throw new Error(error);
var data = JSON.parse(body);
var quizzes = data['entities'];
console.log(quizzes);
// Going the all the quzzies we found
for(quiz in quizzes) {
card = quiz['card'];
console.log(card);
}
});```
CodePudding user response:
Issue #1
Based on your first console.log, it looks like your quizzes
variable is an object....not an array, so you're not iterating over an array of quizzes...you're iterating over an object...make sure your quizzes
variable is an array first.
Issue #2
When you use the for...in
statement, the quiz
variable is going to return the name of each property in the object.
So if I just console.log the quiz variable, like this:
for(quiz in quizzes) {
console.log(card);
}
I would see all the properties logged to the console, like this:
type
title
description
slug
.....
Solution: Use a for...of
statement
When you use a for...of
statement, the variable you set (quiz
in your case) is the entire object.
So if I just console.log the quiz variable, like this:
for(const quiz of quizzes) {
console.log(quiz);
}
I would see this:
card: {
type: 'quiz',
title: 'Viet Nam War',
description: 'Viet Nam War',
slug: 'viet-nam-war',
draftExists: false,
number_of_questions: 37,
creator: '01f63a9f-737b-4f59-b7f5-af10b78ce904',
creator_username: 'Mr.TBrown',
creatorPrimaryUsageType: 'SCHOOL',
creator_avatar: {},
badges: [],
visibility: 1,
locked: false,
writeProtection: false,
last_edit: [Object],
featured: false,
young_featured: false,
sponsored: false,
moderation: [Object],
draft: false,
combined: false,
compatibility_level: 6,
sample_questions: [Array],
number_of_plays: 65,
number_of_players: 169,
total_favourites: 0,
question_types: [Array],
created: 1527781306997,
modified: 1528072275324,
access: [Object],
duplication_disabled: false,
uuid: 'cea8c609-ee88-4f11-b146-e8c70f302f14'
}
See the following MDN docs for more examples:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
CodePudding user response:
If you wanna loop over on array you normally want to use for...of
instead of for...in
and also you need to add a variable declaration (const or let).
for(const quiz of quizzes) {
card = quiz['card'];
console.log(card);
}