Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda