Back

[SOLVED] AppwriteException: Invalid `email` param: Value must be a valid email address

  • 0
  • Self Hosted
  • Web
piyushkdas
30 Sep, 2023, 05:47
TypeScript
import React, { useState } from "react";
import { account } from "../appwrite/appwriteConfig";
import { useNavigate } from "react-router-dom";
import { v4 as uuidv4 } from "uuid";
import "../styles/Login.css";

function Register() {
  const navigate = useNavigate();
  const [user, setUser] = useState({
    name: "",
    email: "",
    password: "",
  });

  const isValidEmail = (email) => {
    // Regular expression for a simple email validation
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  };

  const registerUser = async (e) => {
    e.preventDefault();

    if (!isValidEmail(user.email)) {
      console.log("Invalid email address");
      return;
    }

    const promise = account.create(
      uuidv4(),
      user.name,
      user.email,
      user.password
    );

    promise.then(
      (res) => {
        console.log(res);
        navigate("/todolist");
      },
      (err) => {
        console.log(err);
      }
    );
  };

  return (
    <div className="landing">
      <div className="register">
        <h2>Register</h2>
        <form onSubmit={registerUser}>
          <label htmlFor="name">Name: </label>
          <input
            type="text"
            onChange={(e) => setUser({ ...user, name: e.target.value })}
          />
          <label htmlFor="email">Email: </label>
          <input
            type="email"
            onChange={(e) => setUser({ ...user, email: e.target.value })}
          />
          <label htmlFor="password">Password: </label>
          <input
            type="password"
            onChange={(e) => setUser({ ...user, password: e.target.value })}
          />
          <button type="submit">Register</button>
          <p>Already an user?</p>
          <button onClick={()=>navigate('/')}>Login</button>
        </form>
      </div>
    </div>
  );
}

export default Register;
TL;DR
The issue is with the order of parameters in the `account.create` call. The correct order should be: - id - email - password - name The updated code that fixes the issue is as follows: ``` import React, { useState } from "react"; import { account } from "../appwrite/appwriteConfig"; import { useNavigate } from "react-router-dom"; import { v4 as uuidv4 } from "uuid"; import "../styles/Login.css"; function Register() { const navigate = useNavigate(); const [user, setUser] = useState({ name: "", email:
safwan
30 Sep, 2023, 05:49
TypeScript
const promise = account.create(
  uuidv4(),
  user.name,
  user.email,
  user.password
);

This is not the correct format. Have a look at the docs for the correct format.

safwan
30 Sep, 2023, 05:50

Any reason why you are using the uuid package instead of the ID.unique() helper that comes with the appwrite sdk?

safwan
30 Sep, 2023, 05:51
TypeScript
const promise = account.create(
  ID.unique(),
  user.email,
  user.password,
  user.name,
);
piyushkdas
30 Sep, 2023, 06:05

I was following a tutorial. In the tutorial, he was using a older version of appwrite i guess. Ok i will see the docs and give a try.

piyushkdas
30 Sep, 2023, 09:57

still getting the same error

TypeScript
AppwriteException: Invalid `email` param: Value must be a valid email address
    at Client.<anonymous> (http://localhost:3000/static/js/bundle.js:49742:17)
    at Generator.next (<anonymous>)
    at fulfilled (http://localhost:3000/static/js/bundle.js:49376:24)
safwan
30 Sep, 2023, 13:28

Which version of Appwrite are you using?

piyushkdas
30 Sep, 2023, 13:34

13.0.0 latest i guess

safwan
30 Sep, 2023, 13:38

I mean the server version.

piyushkdas
30 Sep, 2023, 13:48

1.4.3

safwan
30 Sep, 2023, 13:50

Just to be sure, can you share the updated code again?

piyushkdas
30 Sep, 2023, 13:57
TypeScript
import React, { useState } from "react";
import { ID } from 'appwrite'
import { account } from "../appwrite/appwriteConfig";
import { useNavigate } from "react-router-dom";
import "../styles/Login.css";

function Register() {
  const navigate = useNavigate();
  const [user, setUser] = useState({
    name: "",
    email: "",
    password: "",
  });

  const registerUser = async (e) => {
    e.preventDefault();

    const promise = account.create(
      ID.unique(),
      user.name,
      user.email,
      user.password
    );

    promise.then(
      (res) => {
        console.log(res);
        navigate("/todolist");
      },
      (err) => {
        console.log(err);
      }
    );
  };

  return (
    <div className="landing">
      <div className="register">
        <h2>Register</h2>
        <form onSubmit={registerUser}>
          <label htmlFor="name">Name: </label>
          <input
            type="text"
            onChange={(e) => setUser({ ...user, name: e.target.value })}
          />
          <label htmlFor="email">Email: </label>
          <input
            type="text"
            onChange={(e) => setUser({ ...user, email: e.target.value })}
          />
          <label htmlFor="password">Password: </label>
          <input
            type="password"
            onChange={(e) => setUser({ ...user, password: e.target.value })}
          />
          <button type="submit">Register</button>
          <p>Already an user?</p>
          <button onClick={()=>navigate('/')}>Login</button>
        </form>
      </div>
    </div>
  );
}

export default Register;
safwan
30 Sep, 2023, 14:01

the order is wrong

safwan
30 Sep, 2023, 14:01

it needs to be

  • id
  • email
  • password
  • name
safwan
30 Sep, 2023, 14:02

you’ve done

  • id
  • name
  • email
  • password
safwan
30 Sep, 2023, 14:02

make sure the order is correct in the account.create call

piyushkdas
30 Sep, 2023, 14:05

ohhhh.. thanks dude you saved the day 🥲

safwan
30 Sep, 2023, 14:19

[SOLVED] AppwriteException: Invalid email param: Value must be a valid email address

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more