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
?– BrTkCa
Exactly, I passed up the setup, app.use(bodyParser.json()); When I send something (using POSTMAN) I pass Content-Type: application/json
– Rafael Augusto
That stretch is in the
app.js
? Apparently using express, could try to pass straightapp.use(bodyParser.json());
andapp.use(bodyParser.urlencoded({extended: true}));
in the app.js, maybeconfigureExpress
is not being called.. tested this?– BrTkCa
@Lucascosta yes, it’s on Server.js, and I tested yes, and it’s running
– Rafael Augusto
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.– BrTkCa
He already is, I put only there in the example, but in the code he is
– Rafael Augusto
Are you sending a GET or a POST? you can show HTML/Javascript?
– Sergio
@Sergio I am sent POST, using POSTMAN to test the API
– Rafael Augusto
What gives
console.log(req.body);
?– Sergio
@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.
– Rafael Augusto
Displays HTML and Javascript if any
– Sergio
@Sergio don’t have HTML or JS, all I have is the JS of the API (Node), the tests I do is via Postman
– Rafael Augusto
Ah, truth you said, I forgot. I think Postman is sending a GET. Are you sure you’re getting a POST?
– Sergio
Now that Voce spoke, it only inserts the way it spoke up, when I send GET, if I send post, the value remains empty
– Rafael Augusto