T
T
Timezel2021-05-20 19:14:56
JavaScript
Timezel, 2021-05-20 19:14:56

Uncaught SyntaxError: Unexpected token, in JSON at position 7?

I am writing code for an online store, I ran into the problem
Uncaught SyntaxError: Unexpected token , in JSON at position 7.

The code itself:

class LocalStorageUtil {
    constructor() {
        this.keyName = 'products';
    }

    getProducts() {
        const productsLocalStorage = localStorage.getItem(this.keyName);
        if (productsLocalStorage !== null) {
            return JSON.parse(productsLocalStorage);
        } 
        return [];
    }

    putProducts(id) {

    }
}

const localStorageUtil = new LocalStorageUtil();

const a = localStorageUtil.getProducts();
console.log(a);


If you need additions, write in the comments

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kentavr16, 2021-05-20
@Kentavr16

As far as I understand, it is not possible to parse the value from local storage into json. You need to see what you have there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question