[SOLVED] How can I save email on Moralis db?

I use Web3Auth for user authentication. I can retrieve user’s email from the localStorage.
I could’t figure out how to save the email on Moralis db. Here is the code.

let openlogin_store = localStorage.openlogin_store;
        console.log("openlogin_store:",openlogin_store);

let email = JSON.parse(openlogin_store).email;
        console.log("email:", email)

let user = new Moralis.User();
        user.set("email", `${email}`);

Last part is not executed. So, I found no email saved on dashboard.

How can I fix this?
Thank you.

You also have to save the jar object with .save()

Thank you!
I have modified the code as follows:

let user = new Moralis.User();
user.set("email", `${email}`);
user.save().then(
  (user) => {
    alert("New object created with objectId: " + user.id);
  },
  (error) => {
    alert("Failed to create new object, with error code: " + error.message);
  }

and getting the error like this:

It seems I didn’t catch the user’s object. Could you point me to the right direction?

you don’t have to create a new user, you have to use current user object from current authenticated user

How can I get the current user object?
I get null in return with this:

let currentUser = Moralis.User.current();
console.log("currentUser:", currentUser);

Sorry, I’m quite new in this space yet.

documentation is available here: https://v1docs.moralis.io/moralis-dapp/users/current-user

is the user authenticated?

Thank you so much for clarifying the problem.

I think I had to include the code within the scope of web3() in my case.

This is now working:

async function web3() {
        const user = await Moralis.authenticate({
          provider: 'web3Auth',
          clientId: 'BLSlQ_QjUEZlLOCvnyFzvPfL24FL2Nh7aQPFVvGMi-gXE7y7H2NOdjH589kfVvMtT6VtGt66S76O9WJLbokuu64',
        });
        const openlogin_store = localStorage.openlogin_store;
        const email = JSON.parse(openlogin_store).email;
        user.set("email", `${email}`);
        await user.save()
      };
1 Like