Body NULL Nodejs

Asked

Viewed 452 times

0

I’m starting with nodejs, but whenever I try to send (email and password) to the API via POST, the entered values are returned as NULL.

router.post("/users", function(req, res){
        console.log(req.body)
        let query = "INSERT INTO ??(??,??) VALUES (?,?)";
        let table = ["user_login","user_email","user_password", req.body.email, md5(req.body.password)];
        query = mysql.format(query, table);
        console.log(query)
        connection.query(query, function(err, rows){
            if(err){
                res.json({"Error": true, "Message": "Erro ao executar a query do MYSQL"})
            }else{
                res.json({"Error" : false, "Message": "Usuario adicionado!"});
            }
        })
    });


REST.prototype.configureExpress = function(connection){
    let self = this;
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(bodyParser.json());

    let router = express.Router();
    app.use('/api', router);

    let rest_router = new rest(router, connection, md5);
    self.startServer();
}
  • Are JSON and content-type in POST valid? Usually this problem is due to the absence of body parser. How is your app.js?

  • Exactly, I passed up the setup, app.use(bodyParser.json()); When I send something (using POSTMAN) I pass Content-Type: application/json

  • That stretch is in the app.js? Apparently using express, could try to pass straight app.use(bodyParser.json()); and app.use(bodyParser.urlencoded({extended: true})); in the app.js, maybe configureExpress is not being called.. tested this?

  • @Lucascosta yes, it’s on Server.js, and I tested yes, and it’s running

  • I’m sorry to keep asking questions and asking for tests, but it’s the things that might be causing the problem @Rafaelaugusto. Try putting configureExpress block first of all, before the routes.

  • He already is, I put only there in the example, but in the code he is

  • Are you sending a GET or a POST? you can show HTML/Javascript?

  • @Sergio I am sent POST, using POSTMAN to test the API

  • What gives console.log(req.body);?

  • @Sergio Vazio "{}", I was able to recover the value so req.query.email, but I think it is not the correct form, so I would like to be able to discover the source of the problem.

  • Displays HTML and Javascript if any

  • @Sergio don’t have HTML or JS, all I have is the JS of the API (Node), the tests I do is via Postman

  • Ah, truth you said, I forgot. I think Postman is sending a GET. Are you sure you’re getting a POST?

  • Now that Voce spoke, it only inserts the way it spoke up, when I send GET, if I send post, the value remains empty

Show 9 more comments

2 answers

1

At Postman, change your Headers for Content-Type: application/json:

screenshot dos headers do postman

Change in Body for raw:

screenshot do body do postman

And, doing the Post, the answer came:

screenshot da resposta no postman

Here’s the code I used for testing - server.js:

var express = require("express");
var bodyParser  = require("body-parser");
var app  = express();
var rest = require("./rest.js");
var router = express.Router();
app.use('/api', router);

function REST(){
    var self = this;
    self.configureExpress();
};

REST.prototype.configureExpress = function(connection){
    let self = this;
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(bodyParser.json());

    let router = express.Router();
    app.use('/api', router);

    let rest_router = new rest(router, connection);
    self.startServer();
}

REST.prototype.startServer = function() {
      app.listen(3000,function(){
          console.log("servidor trakinas");
      });
}

new REST();

E Rest.js:

function REST_ROUTER(router,connection) {
    var self = this;
    self.handle_routes(router,connection);
}

REST_ROUTER.prototype.handle_routes = function(router,connection) {
    router.get("/bolacha", function(req,res) {
        res.json({"sabor" : "uva"});
    })

    router.post("/bolacha", function(req, res){
        res.send(200, req.body);
    });
}

module.exports = REST_ROUTER;

The source for that answer is here.

0


I was able to fix it, the mistake was actually from POSTMAN. just setting the way to pass parameters, he was sending GET, even if I switched to POST

  • 1

    Explain better how you solved your problem, and accept your own answer, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.