Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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

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;
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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};

});
about 4 years ago · Santiago Trujillo Report

0

In the user router file, please use the ' await ' keyword before these methods.

registerStudent(data);
loginUser(data);

In the example, you can use

userRouter.post("/user/add", async ctx => {
    const data = ctx.request.body;
    const student = await registerStudent(data);
    ctx.body = {success:true};
    ctx.status = 201;
    ctx.set('Content-Type', 'application/json');
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!