Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda