0
votes
this is my code 
usercontroller:-
const {hashSync,genSaltSync}=require('bcrypt');
const {create}=require('./user.service');
module.exports={
    createUser:(req,res)=>{
        const body=req.body;
        const salt=genSaltSync(10);
        body.password=hashSync(body.password,salt);
        create(body,(error,results)=>{
            if(error){
                return res.status(500).json(
                    {
                        success:0,
                        message : "Database connection error"
                    }
                );
            }
            return res.status(200).json({
                status:200,
                data:results
            });
        })
    }

}

userservice:-
const pool=require("../config/database")
module.exports={
    create: (data,callback)=>{
        pool.query(
            `insert into registration(firstName,lastName,gender,email,password,number)
                values(?,?,?,?,?,?)`,
                [
                    data.first_name,
                    data.last_name,
                    data.gender,
                    data.email,
                    data.password,
                    data.number
                ],
                (error,results,fields) =>
                {
                    if(error)
                    {
                        return callback(error);
                    }
                    else{
                        return callback(null,results);
                    }
                }
        );
    }
}
userrouter:-
const {createUser} = require("../users/user.controller")
const {createUser}=require("./user.controller")
const router=require("express").Router;
router.post("/",createUser);
module.exports=router;

app.js
require("dotenv").config();
const express = require("express")
const app=express();
const userrouter=require('./users/user.router')

app.use("/api/users",userrouter);
app.listen(process.env.APP_PORT,()=>{
    console.log("server up and running")
});


.env
APP_PORT=3000
DB_PORT=3306
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=divyanshu123
MYSQL_DB=test

i am not able to run it getting error:-

[nodemon] 2.0.4 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: js,mjs,json [nodemon] starting node app.js C:\Users\Divyanshu Thakur\Desktop\Jwt\node_modules\express\lib\router\index.js:502 this.stack.push(layer); ^

TypeError: Cannot read property 'push' of undefined at Function.route (C:\Users\Divyanshu Thakur\Desktop\Jwt\node_modules\express\lib\router\index.js:502:14) at Function.proto. [as post] (C:\Users\Divyanshu Thakur\Desktop\Jwt\node_modules\express\lib\router\index.js:509:22) at Object. (C:\Users\Divyanshu Thakur\Desktop\Jwt\users\user.router.js:4:8) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Module.require (internal/modules/cjs/loader.js:1019:19) at require (internal/modules/cjs/helpers.js:77:18) at Object. (C:\Users\Divyanshu Thakur\Desktop\Jwt\app.js:4:18) [nodemon] app crashed - waiting for file changes before starting...

2

2 Answers

1
votes

Change this:

const router=require("express").Router;

to this:

const router=require("express").Router();

You have to actually call the factory function to create a new router. Your problem then occurs when you try to do:

router.post("/",createUser);

or:

app.use("/api/users",userrouter);

But, your object isn't actually a router.

0
votes

Not sure if this is the source of your issue but this seems rather problematic:

const { createUser } = require("../users/user.controller")
const { createUser } = require("./user.controller")

That's from the userRouter file.