skip to Main Content

I ran into this problem website when am following this video tutorial until here: https://youtu.be/nGoSP3MBV2E?list=PLIZkq64ad9C4POK0E1fhCaxlxTXwr7wSh&t=5976.

The problem seem to arised from these 2 files:
error from register/route.js & models/User.js

Here’s the source code of those 2 files:

register/route.js:
”’

  import mongoose from "mongoose";

    import {User} from "@/models/User";;

    export async function POST(req){
    const body = await req.json();
    mongoose.connect(process.env.MONGO_URL);
    const cretedUser = await User.create(body);
    return Response.json(createdUser);
}

”’

Model/User.js:

”’

    import {model, models, Schema} from "mongoose";

    const UserSchema = new Schema({
    email:{type:String, required:true, unique: true},
    password:{
        type:String, 
        required: true, 
        validate: pass => {
        if(!pass?.length || pass.length <5 ){
            new Error('password must be at least 5 characters');
        }
    },
    },
    },{timestamps: true}); 

export const User = models?.User || model('User', UserSchema);

”’

I have tried correcting any typo as well as fixing any incorrect path but nothing worked so far, here’s how my repo look like:Repo

2

Answers


  1. Chosen as BEST ANSWER

    Simply changing from @/src/app/models/User.js to import {User} from "/src/app/models/User.js"; work for me


  2. This is a Next.js app and the User.js file is located in src/app/models/User.js.

    The import should be import {User} from "@/src/app/models/User.js";

    Read more about Next.js Path Aliases here: https://nextjs.org/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search