[Object Object] is not a valid JSON

I am getting this error when trying to reset a password.

and the code is

document.addEventListener("DOMContentLoaded", async function(event) {
	let formNotice = document.querySelector('.form-notice');
	let sbs = sessionStorage.sbsession;
	if (!Boolean(sbs)) {
		formNotice.classList.remove('notice-info', 'hide')
		formNotice.textContent = "Your password reset link is invalid or has expired. Please <a href='auth/reset-password'>Try Again</a>."
	} else {
  	sessionData = JSON.parse(sbs);
    let passwordForm = document.querySelector('#wf-form-Update-Password');
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'email';
    input.value = sessionData.user.email;
    passwordForm.addEventListener('submit', function(e){
    let passwordFormBtn = document.querySelector('.form-submit')
    passwordFormBtn.addEventListener('click', function (e) {

The problem occuring at line

sessionData = JSON.parse(sbs);

I don’t know how to figure it out.

indication is that sbs is an object already, so it wouldnt need parsing.

Well since you’re getting sbs is from the session storage, it must be a string (or undefined, for which you’re testing). You probably forgot to JSON.stringify() the data when setting it to the session storage though, which would indeed just store the string "[object Object]" then.

Thanks for the answer. I got my problem fixed. I just removed the sessionData variable and got the user email by getItem property. It worked for me.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.