Retrieving specific objects from a Fetch API (JavaScript) - Re post [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Edited question: Trying to write the code needed to display only the title and description of the book returned.
I have attempted the following, and am met with a response of, "Cannot read property 'title' of undefined." The same happens to 'description'(line 6) when line 5 is removed.
require('isomorphic-fetch');
let items = [];
fetch("https://www.googleapis.com/books/v1/volumes?q=isbn:0747532699")
.then(res => res.json())
.then((result) => {
items = result.items;
console.log(items.volumeInfo.title)
console.log(items.volumeInfo.description)
}),
(error) => {
console.log(error);
}
What am I doing wrong, and how can I fix it?

items is an array. You would need to access it like this:
result.items[0].volumeInfo.title
More specifically, you could loop the results
for (let i = 0 in items) {
result.items[i].volumeInfo.title; //do something here
}

The issue here is result.items is actually an array not an object. Thus we can not simply call volumeInfo property on an array. If you want first array values then you can simply do:
const items = result.items;
if(items && items.length){
console.log(items[0].volumeInfo.title)
console.log(items[0].volumeInfo.description)
}
Or, if you want the info from all the items, then you can loop over it like:
const items = result.items;
items.forEach(function(obj,index){
console.log(obj.volumeInfo.title)
console.log(obj.volumeInfo.description)
});

Related

Filter and Include don't work together as expected [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
So I have a clients that I'm iterating, then each client has some vehicles as an array represnted in strings, What I want to do is iterate the clients and check if a specific id is included in any of the .vehicles key, but I'm not getting the wanted results, check my function:
const v = clients.clients.filter((c) => {
if (c.vehicles.includes(fields.customer) === true) return c;
});
You're making it more complicated than needed. You need to return true or false inside filter so:
const v = clients.clients.filter((c) =>
c.vehicles.includes(fields.customer)
);
Then you have a list of clients that have your values. At last your probably use a map to get an array with the desired results.
I'm guessing you want to have all vehicles so the next line is:
let vehicles = v.map(c => c.vehicles)
An array filter callback should return a boolean, not the item itself
You can try this way
const v = clients.clients.filter((c) => c.vehicles.includes(fields.customer));
You don't need to put a new if condition in your filter.
The filter callback function takes the validation condition directly
const result = words.filter(word => word.length > 6) ;
As you can see above (cf mdn) , the filter method will return a new array with only words that have more than 6 letters.
So, in your case, your function should be only
const v = clients.clients.filter(c => c.vehicles.includes(fields.customer)) ;
Also, I think you need to be careful with the key name, here you have a clients key in an object that has the same name, it's not really understandable, maybe you can have carFleet.clients.

How te get an element from .fields [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am having trouble getting the property filename from the req.files that I get from the router. Here's what I get:
And here's how I've been trying to get the filename of each (I am only using 2 pictures in this example but I could get more than two images so that's why I am iterating with the forEach)
let arrayImages = [];
if (req.files) {
Array(req.files).forEach(image => {
arrayImages.push(image[0].filename);
})
}
Hi everyone thanks for all the help, i finally figured it out!
let arrayImages = [];
for (const clave in req.files) {
array = req.files[clave]
arrayImages.push(`${array[0].filename}`);
}
that way i've got the fieldname of each element
The root of your data from screen is an object.
So try to code like that:
let arrayImages = [];
if (req.files) {
Object.values(req.files).forEach(arr => {
arrayImages.push(arr[0].filename);
})
}

How to check if all elements in a node list have the same class or the same style property value? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I created memory game. The only problem is that when the game is done and the player wins, it doesn't console.log('win)
Code:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every((each)=>{
each.classList.contains('matched')
})
if(check == true){
console.log('win')
}
Inside your every method, you're only checking for the class's existence, not actually returning anything.
You have to write it like this:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every(item => item.classList.contains('matched'))
console.log(check)
Or like this, if you want to stick to your original answer:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every((item) => {
return item.classList.contains('matched')
})
console.log(check)

how can we get the docs in users collection in firebase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i want to create an array from data coming from firestore.
the structure is as follows
users(collection)>uid1(doc)>(profile)>uid2(doc)>data
now the uid is dynamic as for every element of users collection. i want to create an array for every element with data
eg
[
{data},
{data},
.....
]
i have written the code for single uid
db.collection('users').doc("BbXn5M213XSKNfcSAR2MvbjPD403").collection("profile").onSnapshot((snapshot) => {
console.log(snapshot.docs.map(doc => doc.data()));
});
here are some firebase screenshot
screenshot 1 screenshot 2 here
I think what you'd looking for might be a collection group query, which is a way to get contents from all collections with a specific name.
So in your example this would be the way to get all documents from all data (sub)collections:
db.collectionGroup("profile").onSnapshot((snapshot) => {
console.log(snapshot.docs.map(doc => doc.data()));
});

Adding a for loop within a variable declaration [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im having trouble looping through the array tabData and storing the new array into 'filteredData'
const filteredData = allData.filter(
({ class }) => tab === tabData[1].tab && class === tabData[1].label,
);
tabData contains the following 0:{Tab:1, Label:'firstTab'} 1:{Tab:2 , Label:'secondTab'} ... and so on
1) You cant use 'class' is reserved word.
2) I guess code should look like:
const filteredData = allData.filter(tab => tab.label === OTHER.label)
Where "OTHER.label" filtering for that label
You’d better take a look at Array.prototype.filter ’doc
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
see details

Categories

Resources