Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I check API endpoints using POSTMAN. But its post HTTP method is not working properly

I check API endpoints using POSTMAN. But its post HTTP method is not working properly. This is the user login backend code. Get data from the mongo DB database and validated the user login. After validating user can easily log in to the system.

This is the User Dao.js file.

userDAO.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const user = require('./index').db("newDb").collection("user");
const bcrypt = require('bcrypt');

var ObjectId = require('mongodb').ObjectId;

const register = ({userName, password, age, city}) => {

    bcrypt.hash(password, 10, function(err, hashPassword) {
        const insertResult = user.insertOne({userName, password, hashPassword, age, city});
        return insertResult;
    });

};

const login = async({userName, password}) => {

    const filteredDocs = await user.findOne({ userName: userName });
    
    if(filteredDocs){
        if(filteredDocs.password === password){
            return {success:true, msg: "Login Successfull"};
        }else{
            return {success:false, msg: "Password Incorrect Try Again"};
        }
    }else{
        return {success:false, msg: "Username Incorrect Try Again"};
    }
};

module.exports = {register, login};

This is the user.api file.

user.api.js

const {register, login} = require('../dal/user.dao');

const registerStudent = async({userName, password, age, city}) => {
    const user = {
        userName,
        password,
        age,
        city
    }
    return await register(user);
};

const loginUser = async({userName, password}) => {
    const user = {
        userName,
        password
    }
    return await login(user);
};

module.exports = {registerStudent, loginUser};

This is the userRouter.js file.

user.Router.js

const Router = require('@koa/router');

const userRouter = new Router();

const {registerStudent, loginUser} = require('../api/user.api');

userRouter.post("/user/add", async ctx => {
    const data = ctx.request.body;
    const student =  registerStudent(data);
    ctx.body = {success:true};
    ctx.status = 201;
    ctx.set('Content-Type', 'application/json');
});

userRouter.post("/user/login", async ctx => {
    const data = ctx.request.body;
    const student =  loginUser(data);
    ctx.body = {status: student};
    ctx.status = 201;
    ctx.set('Content-Type', 'application/json');
});

module.exports = userRouter;

>Solution :

You appear to be setting the response to the request…

try this:

userRouter.post("/user/add", async (ctx, res) => {
    const data = ctx.request.body;
    const student =  registerStudent(data);
    

    res.json({success:true});
    
});

userRouter.post("/user/login", async (ctx, res) => {
    const data = ctx.request.body;
    const student =  loginUser(data);

    res.json({status:student};

});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading