Knex NodeJS and inserting into the database

0 votes

Here is my first attempt at setting up an API server using nodejs, because I'm new to it. Instead of MongoDB, I preferred to utilise MySQL.

My issue is that the database doesn't appear to want to be saved by the statement "knex('user').insert('email: req.body.email');"

var dbConfig = {
  client: 'mysql',
  connection: {
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'db_nodeapi'
  }
};
var express = require('express');                       // call express
var bodyParser = require('body-parser');                // call body-parser
var knex = require('knex')(dbConfig);                   // set up database connection
var app = express();                                    // define our app using express
app.use(bodyParser.urlencoded({ extended: true }));     // configure app to use bodyParser() 
app.use(bodyParser.json());                             // this will let us get the data from a POST
var router     = express.Router();                      // get an instance of the express Router
router.use(function(req, res, next) {                   // middle ware for authentication
    console.log(' -Logging- ');
    next();                                             // continue to next route without stopping
});
router.get('/', function(req, res) {                    // listen for a post on root
    res.json({ message: ' -Success- ' });   
});
router.route('/user')                                   // set up user route
    .post(function(req, res) {                          // listen for a post on user
        console.log(' -Post -');                        // report a post
        knex('user').insert({email: req.body.email});   // insert user into user table
        res.json({ success: true, message: 'ok' });     // respond back to request
    });
app.use('/api', router);                                // register routes beginning with /api  
var port = process.env.PORT || 8080;                    // set server port number
app.listen(port);                                       // setup listener
console.log('Magic happens on port ' + port);           // report port number chosen

Problem is I can't get knex to add to the database!

CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
  `email` varchar(255) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Here is the database

Jun 27 in Node-js by Vaani
• 7,020 points
14 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,740 points
8,459 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,740 points
2,346 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,740 points
4,366 views
0 votes
1 answer

How to split and modify a string in NodeJS?

Hello @kartik, Use split and map function: var str = "123, 124, 234,252"; var ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,740 points
382 views
0 votes
1 answer

nodejs mysql Error: Connection lost The server closed the connection

Try to use this code to handle server disconnect: var ...READ MORE

answered Jun 17 in Node-js by Neha
• 8,740 points
555 views
0 votes
0 answers

Remove double quotes from a Table Name using SEQUELIZE Nodejs

used Nodejs' SEQUELIZE to construct an Account ...READ MORE

Jun 27 in Node-js by Vaani
• 7,020 points
50 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,007 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP