0

I'm trying to Insert some data to my database(mysql) with nodejs and I already did make some code but in postman it displays Could not get any response even though I know that I followed properly some tutorials that I watched.

Here's my code

SendOrder.js (models)

var db=require('../dbconnection');

var Task = {
    addTask:function(Task,callback){
        return db.query("Insert into orders ( order_id, order_no, tbl_id, menu_id, \
            order_quantity, order_discount, order_type, \
            order_amount, menu_name, menu_price ) values(?,?,?,?,?,?,?,?,?,?)",
        [
            Task.order_id, Task.order_no, Task.tbl_id, Task.menu_id,
            Task.order_quantity, Task.order_discount, Task.order_type,
            Task.order_amount, Task.menu_name, Task.menu_price
        ], callback);
    },
}

module.exports=Task;

SendOrder.js (router)

var express = require('express');
var router = express.Router();
var Task = require('../models/SendOrder');

router.post('Send/', function(req, res, next){
        Task.addTask(req.body,function(err,count){
            console.log(req.body);
            if(err)
            {
                res.json(err);
            }
            else{
                res.json(req.body);
            }
        });
});

module.exports = router;

EDIT:

dbconnection.js

var mysql=require('mysql');
var connection=mysql.createConnection({

    host: 'localhost',
    user: 'root',
    password: '',
    database: 'opob',

});

module.exports=connection;

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mysql = require('mysql');
var connection = require('express-myconnection')

var SendOrder = require('./routes/SendOrder');           // SendOrder

var app = express();  

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/SendOrder', SendOrder);                        // SendOrder

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Postman

Violet
  • 115
  • 4
  • 15
  • You are using `http://XXX.XXX/SendOrder/Send` API from postman. Should you not use only `http://XXX.XXX/Send ` API? As you have defined API to your `SendOrder.js` file as `router.post('Send/', function(req, res, next)` – Hardik Shah Jul 10 '18 at 17:59
  • As @vizsatiz mentioned, that would be 401. I got wrong. – Hardik Shah Jul 10 '18 at 18:15

2 Answers2

0

From whatever you have shared here are the following possibilities:

  1. Your IP/PORT that you are trying to hit is wrong. Please cross verify them again.
  2. The IP that you are trying to hit is not accessible from the machine where postman is installed (I added this possibility as you are using IP instead of localhost)
  3. The third possibility would be server crashing when you hit the Send/ API. If the problem is with your code, most probably this is the reason. In this case, you can check the server console to find crash logs and stack-trace.

As @Hardik has mentioned in the comments is not a wrong URL as that would return 404.

vizsatiz
  • 1,933
  • 1
  • 17
  • 36
0

Go to Setting in Postman

  1. Off the SSL certificate verification in General tab: (Second option under Request) enter image description here

  2. Off the Global Proxy Configuration and Use System Proxy in Proxy tab: enter image description here

If both not work, try below code:

if(err)
{
    return res.status(500).json(err);
}
else{
    return res.status(200).json(req.body);
}

Hope, this may help you!

=========== EDITED ==============

Looking at your app.js file. It seems you need to use body-parser package to parse the JSON data from request.

npm install body-parser --save

body-parser extract the entire body portion of an incoming request stream and exposes it on req.body.

Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
  • Sir, the screen shot gives me error 404 when I send in postman. – Violet Jul 11 '18 at 03:09
  • Try to implement code that I have mentioned with existing setting. – Hardik Shah Jul 11 '18 at 03:39
  • Enable "User proxy setting" and try once again. I am going to update the screenshot. – Hardik Shah Jul 11 '18 at 03:48
  • you mean, "Use System Proxy" sir? if yes, nothing is change. I'm sorry sir, I'm just a student not a pro. :( – Violet Jul 11 '18 at 03:55
  • That is totally fine. I am wondering actually why this does not work for you. – Hardik Shah Jul 11 '18 at 03:57
  • Can you please share your `mysql` connection code. from where you exprorting connection object. `dbconnection.js` file. – Hardik Shah Jul 11 '18 at 04:00
  • yes sir, I've update my post and I also add my app.js. I understand you sir, like I said in my post, I know I follow some tutorial properly but it's not giving the expected output. – Violet Jul 11 '18 at 04:06
  • Are you sure you get the whole JSON from postman to NodeJS as you should use "body-parser" to parse the JSON from `req.body` take a look here https://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express – Hardik Shah Jul 11 '18 at 04:23