function getResults(querySearch) {
api.pageNum += 1;
refs.list.innerHTML = '';
const imgBlock = fetch(
`https://pixabay.com/api/?image_type=photo&orientation=horizontal&q=${querySearch}&page=${api.pageNum}&per_page=12&key=${api.key}`,
)
.then(data => {
return data.json();
})
.then(pic => pic.hits)
.map(item => template(item).join(''));
refs.list.insertAdjacentHTML('beforeend', imgBlock);
}
I'd like to use a handlebars template to each item mapped, but for some reason, I keep getting:
"fetch(...).then(...).then(...).map is not a function"
Here's a full code:
import './styles.css';
import template from './templates/template.hbs';
const api = {
key: '#',
querySearch: '',
pageNum: 1,
};
const refs = {
list: document.querySelector('.gallery'),
form: document.querySelector('#search-form'),
input: document.querySelector('input'),
};
refs.input.addEventListener('input', catchInput);
function catchInput(event) {
console.log(event.target.value);
getResults(refs.input.value);
console.log(refs.input.value);
}
function getResults(querySearch) {
api.pageNum += 1;
refs.list.innerHTML = '';
const imgBlock = fetch(
`https://pixabay.com/api/?image_type=photo&orientation=horizontal&q=${querySearch}&page=${api.pageNum}&per_page=12&key=${api.key}`,
)
.then(data => {
return data.json();
})
.then(pic => pic.hits)
.map(item => template(item).join(''));
refs.list.insertAdjacentHTML('beforeend', imgBlock);
}
HANDLEBARS
<div class="photo-card">
<img src="{{webformatURL}}" alt="{{webformatURL}}" />
<div class="stats">
<p class="stats-item">
<i class="material-icons">thumb_up</i>
{{likes}}
</p>
<p class="stats-item">
<i class="material-icons">visibility</i>
{{visibility}}
</p>
<p class="stats-item">
<i class="material-icons">comment</i>
{{comments}}
</p>
<p class="stats-item">
<i class="material-icons">cloud_download</i>
{{downloads}}
</p>
</div>
</div>