[英]Save multiple objects in a sessionStorage
I am trying to save objects in a sessionStorage anytime I click on a bookmark icon.每当我单击书签图标时,我都会尝试将对象保存在 sessionStorage 中。 But instead of saving several objects.
但不是保存几个对象。 Anytime I click on my icon bookmark to save a book;
任何时候我点击我的图标书签来保存一本书; it is being overridden by the new value.
它被新值覆盖。
How could I add several objects (books) in my sessionStorage?如何在 sessionStorage 中添加多个对象(书籍)? Thanks
谢谢
here is the code snippet这是代码片段
try {
const responseData = await sendHttpRequest(
'GET',
"https://www.googleapis.com/books/v1/volumes?q=" + search );
const listOfBooks = responseData;
console.log(listOfBooks);
for (i=0; i < listOfBooks.items.length; i++) {
console.log(listOfBooks.items[i]);
const postEl = document.importNode(template.content, true);
console.log("postEl", postEl);
//BookMark Icon event
const target = postEl.querySelector('.icon-bookmark');
target.addEventListener('click', () => {
sessionStorage.setItem('bookStorage', bookStorage);
sessionStorage.setItem('bookStorage' , JSON.stringify(bookStorage));
const extractedBook = sessionStorage.getItem('bookStorage');
const extractedBookInfo = JSON.parse(sessionStorage.getItem('bookStorage'));
console.log(extractedBookInfo);
});
postEl.querySelector('.id').textContent = 'id: '+ listOfBooks.items[i].id;
postEl.querySelector('.titre').textContent = 'Titre: '+ listOfBooks.items[i].volumeInfo.title;
postEl.querySelector('.author').textContent = 'Auteur: '+ listOfBooks.items[i].volumeInfo.authors;
postEl.querySelector('.desc').textContent ='Description: '+ listOfBooks.items[i].volumeInfo.description;
postEl.querySelector('img').src = listOfBooks.items[i].volumeInfo.imageLinks.thumbnail;
if (postEl.querySelector('.desc').textContent.length > 200) {
postEl.querySelector('.desc').textContent = postEl.querySelector('.desc').textContent.substring(0,200);
}
if (!postEl.querySelector('img').src) {
postEl.querySelector('img').src ="resources/css/img/unavailable.png";
}
listElement.append(postEl);
const bookStorage = {
id: listOfBooks.items[i].id,
title: listOfBooks.items[i].volumeInfo.title,
author: listOfBooks.items[i].volumeInfo.authors,
desc: listOfBooks.items[i].volumeInfo.description,
img: listOfBooks.items[i].volumeInfo.imageLinks.thumbnail
};
}
} catch (error) {
alert(error.message);
}
}
btnRechercher.addEventListener('click', fetchBooks, false);
You can only have unique keys inside session storage, that's why everytime you insert a new entity with the same key the storage overwrites the old one.您只能在 session 存储中拥有唯一键,这就是为什么每次插入具有相同键的新实体时,存储都会覆盖旧实体。 You can do a for and save every book, for example 'book_1', 'book_2'.
您可以为每本书执行一次并保存,例如“book_1”、“book_2”。 Or you can store an array, but you have to use json.stringify(name_of_your_array) and then when you want your array back you can use json.parse(storage.getItem(key))
或者你可以存储一个数组,但你必须使用 json.stringify(name_of_your_array) 然后当你想要你的数组时你可以使用 json.parse(storage.getItem(key))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.