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, 2022 in Node-js by Vaani
• 7,070 points
477 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,840 points
13,766 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,874 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,640 views
0 votes
0 answers
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,840 points
4,517 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,840 points
9,080 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, 2022 in Node-js by Neha
• 9,020 points
11,413 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, 2022 in Node-js by Vaani
• 7,070 points
1,541 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,922 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP