Next.js Discord

Discord Forum

What is Cannot redefine bind, Check the error :

Unanswered
Chinese Alligator posted this in #help-forum
Open in Discord
Chinese AlligatorOP
I don't have any "bind" used in my code
I checked the file that the error is occurring from and idk what is wrong
Here is models.js :
"use server"
import { mongoose } from "mongoose";
const Schema = mongoose.Schema()



const userSchema = new mongoose.Schema(
    {
      username: {
        type: String,
        required: true,
        unique: true,
        min: 3,
        max: 20,
      },
      email: {
        type: String,
        required: true,
        unique: true,
        max: 50,
      },
      password: {
        type: String,
      },
      isAdmin: {
        type: Boolean,
        default: false,
      },
    },
    { timestamps: true }
  );



export const User = mongoose.models?.User || mongoose.model("User", userSchema);


and database handle.jsx :
"use server"
import { connectToDB } from ".";
import { unstable_noStore as noStore } from "next/cache";
import { User } from "./models";
import bcrypt from "bcryptjs";



export const getUser = async (id) => {
    noStore();
    try {
      connectToDb();
      const user = await User.findById(id);
      return user;
    } catch (err) {
      console.log(err);
      throw new Error("Failed to fetch user!");
    }
  };


export const getUsers = async () => {
  try {
    connectToDB();
    const users = await User.find();
    return users;
  } catch (err) {
    console.log(err);
    throw new Error("Failed to fetch users!");
  }
};



export const addUser = async (prevState,formData) => {
  const { username, email, password } = Object.fromEntries(formData);

  try {
    connectToDB();
    const newUser = new User({
      username,
      email,
      password,
    });

    await newUser.save();
    console.log("saved to db");
    revalidatePath("/dashboard");
  } catch (err) {
    console.log(err);
    return { error: "Something went wrong!" };
  }
};




export const deleteUser = async (formData) => {
  const { id } = Object.fromEntries(formData);

  try {
    connectToDB();

    await Post.deleteMany({ userId: id });
    await User.findByIdAndDelete(id);
    console.log("deleted from db");
    revalidatePath("/admin");
  } catch (err) {
    console.log(err);
    return { error: "Something went wrong!" };
  }
};



export const register = async (previousState, formData) => {
  const { username, email, password} =
    Object.fromEntries(formData);


  try {
    connectToDB();

    const user = await User.findOne({ username });

    if (user) {
      return { error: "Username already exists" };
    }

    const salt = await bcrypt.genSalt(10);
    const hashedPassword = await bcrypt.hash(password, salt);

    const newUser = new User({
      username,
      email,
      password: hashedPassword,
    });

    await newUser.save();
    console.log("saved to db");

    return { success: true };
  } catch (err) {
    console.log(err);
    return { error: "Something went wrong!" };
  }
};






export const login = async (prevState, formData) => {
  const { username, password } = Object.fromEntries(formData);

  try {
    await signIn("credentials", { username, password });
  } catch (err) {
    console.log(err);

    if (err.message.includes("CredentialsSignin")) {
      return { error: "Invalid username or password" };
    }
    throw err;
  }
};

7 Replies

Chinese AlligatorOP
!
Chinese AlligatorOP
Guys???
West African Crocodile
anyone have any clue about this error because im struggling with it, cant figure out where its breaking my code
West African Crocodile
found the cause holy
how dumb it is
cant add "use server" in schema code lel
:dead: