I have been attempting to store a registered user object inside localstorage. However, when I try to retrieve or set those values from localstorage in an array and parse it back to an object, I encounter an error stating that ".push" or ".some" is not a function.
import { Injectable } from '@angular/core';
import { Cart } from 'src/app/models/cart';
import { Product } from 'src/app/models/product';
import { productAndQuantity } from 'src/app/models/product-and-quantity';
import { User } from 'src/app/models/user';
@Injectable({
providedIn: 'root'
})
export class CustomerService {
registeredUserArray : User[];
tempCart: Cart = new Cart;
purchaseHistoryCartArray: Cart[] = [];
localStorageKey = "registeredUserArray";
defaultUsers : User[] = [
{
username : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfd8ccccffd8d0d0d8d3da91dcd0d2">[email protected]</a>",
password : "BruceWayne@123",
isAdmin : true,
},
{
username : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa8e9f898eba9d979b9396d4999597">[email protected]</a>",
password : "BruceWayne@123",
isAdmin : false,
tempCart: new Cart,
purchaseHistoryCartArray: []
},
]
constructor() {
if (JSON.parse(localStorage.getItem(this.localStorageKey)!))
{
this.registeredUserArray = JSON.parse(localStorage.getItem(this.localStorageKey)! ?? []);
}
else
{
this.registeredUserArray = this.defaultUsers;
localStorage.setItem(this.localStorageKey, JSON.stringify(this.defaultUsers));
}
}
addUser(user : User)
{
// this.registeredUserArray.push(user);
console.log(JSON.parse(localStorage.getItem(this.localStorageKey)!))
this.registeredUserArray.push(user); **<--this is the point of error**
localStorage.setItem(this.localStorageKey, JSON.stringify(this.registeredUserArray));
}