api route
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
hey guys
I'm pretty new to web dev so sorry if this sounds dumb
Im tryin to send a post requst to an api route which is a JSON with a name inside , and then add it to mongodb using mongoose
I've made a user schema and exported a model in a seperate file in compnents foolder in the root dir
when I want to import it using
the schema file :
how im requiring it :
I'm pretty new to web dev so sorry if this sounds dumb
Im tryin to send a post requst to an api route which is a JSON with a name inside , and then add it to mongodb using mongoose
I've made a user schema and exported a model in a seperate file in compnents foolder in the root dir
when I want to import it using
require() I get an err as in the screenshot the schema file :
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name:String
});
module.exports = mongoose.model("user" , userSchema);how im requiring it :
const User = require("@/components/User") ;44 Replies
Use import
import User from "@/components/User"
it's surprising that require doesn't work though
but perhaps related to the TS config
also double check that "components/User" does exist ^^
@Eric Burel it's surprising that require doesn't work though
Sloth bearOP
ive also tried that before and I did once again still the same err
and the path is like this
am I doing something wrong?
Are u exporting?
I read cmponents
not components
Yeah thats also a issue

@Clown Its a js
Sloth bearOP
Yes 🥲
@Eric Burel I read cmponents
Sloth bearOP
The folder name was something ele I quickly changed it to components for the forum, and i didn't check it 🤦ðŸ»
So there is 2 problems
1st the obvious one the name
2nd I shouldn't use js?
So there is 2 problems
1st the obvious one the name
2nd I shouldn't use js?
I think if its a component u should use jsx/tsx
Also the component needs to be exported.
Default exported:
Normal export:
Default exported:
import User from ".."Normal export:
import {User} from ".. "pretty sure this is due to bad absolute path config
require still plays well with absolute path imports, though ofc you should use import statements instead
@Clown Also the component needs to be exported.
Default exported:
`import User from ".." `
Normal export:
`import {User} from ".. "`
Sloth bearOP
Tnx
Can I add you as a friend? 🙃
Can I add you as a friend? 🙃
Barbary Lion
yeah the import problem may have occured as suggested above but I don't get it that why are you importing
User component in route.js file. You may have wanted to import User Model from schema.js or whatever u have named it, and importing right file would work with require(schema.js) as well.@Barbary Lion yeah the import problem may have occured as suggested above but I don't get it that why are you importing `User` component in `route.js` file. You may have wanted to import `User Model` from `schema.js` or whatever u have named it, and importing right file would work with `require(schema.js)` as well.
Sloth bearOP
I was confused too , I checked it again and its working with no problem, others pointed out that the folder name was "cmponent" and I was giving "component" as the path, but before I post it to the forum the naming was all different and I did check the spelling and there was no missmatch , I dont know what caused it either but the problem is resolved for now
Barbary Lion
so what are you using now ?
@Barbary Lion so what are you using now ?
Sloth bearOP
require()
Barbary Lion
path?
Sloth bearOP
"@/components/User"
Barbary Lion
whats inside this file ?
can i see git repo ?
@Barbary Lion whats inside this file ?
Sloth bearOP
it exports a model
Barbary Lion
ok
your folder name in ss is indeed "cmponents"
not in code
but folder name
@Barbary Lion can i see git repo ?
Sloth bearOP
I dont know git but if i did cofigure it out ill send you the repo
Barbary Lion
so u may have changed that folder name, and its working now
Sloth bearOP
yeah its the most reasonable answer
Barbary Lion
btw, if that file exports Model then keep it in separate folder like models/User.js
Sloth bearOP
that would be a good practice
Barbary Lion
because components folder would be supposed to have React components
@Sloth bear that would be a good practice
Barbary Lion
yeah
Sloth bearOP
tnx for the help can I add you as a friend?
Barbary Lion
okay