Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert a new record in nodejs using sequelize POST method

I want to insert a new data in database using sequelize express, without query. I am trying so hard but I didn't get the output... If my code is wrong, then give me a code for insert a new record in db using sequelize express.

const Sequelize = require('sequelize');
var express = require('express');
var app = express();
var mysql = require('mysql');
//var request=require('request')
const sequelize = new Sequelize('ganeshdb', 'root', 'welcome123$', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});
var users = sequelize.define('users', {
    id: {
        primaryKey: true,
        type: Sequelize.INTEGER,
    },
    name: Sequelize.STRING,
    role: Sequelize.STRING,
    email: Sequelize.STRING
});
app.post('/test', function (request, response) {
    return users.create({
        name: request.body.name,
        role: request.body.role,
        email: request.body.email
    }).then(function (users) {
        if (users) {
            response.send(users);
        } else {
            response.status(400).send('Error in insert new record');
        }
    });
});
app.listen(3000, function () {
    console.log('Express server is listening on port 3000');
});
like image 841
GaneSH Avatar asked Sep 04 '18 08:09

GaneSH


People also ask

How do I insert data into a table using Sequelize?

The create() method is used to insert a single row into your SQL table. When you need to insert multiple rows at once, you need to use the bulkCreate() method instead. Finally, you can also write and execute a raw SQL statement using the sequelize. raw() method.

How do I insert multiple data with Sequelize?

Adding multiple rows at once using Sequelize bulkCreate() method. When you need to insert multiple rows to your SQL database table, you can use the Sequelize bulkCreate() method. The bulkCreate() method allows you to insert multiple records to your database table with a single function call.


1 Answers

You should use body-parser

https://www.npmjs.com/package/body-parser

Example use body-parser:

var express = require('express')
var bodyParser = require('body-parser')
 
var app = express()
 
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
 
// parse application/json
app.use(bodyParser.json())
 
app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

Example:

const Sequelize = require('sequelize');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb', parameterLimit: 1000000 }));

const sequelize = new Sequelize('test_01', 'root', 'root', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});

const users = sequelize.define('users', {
    id: {
        primaryKey: true,
        type: Sequelize.INTEGER,
    },
    name: Sequelize.STRING,
    role: Sequelize.STRING,
    email: Sequelize.STRING
});

app.post('/test', function (request, response) {
    return await users.create({
        id: request.body.id,
        name: request.body.name,
        role: request.body.role,
        email: request.body.email
    }).then(function (users) {
        if (users) {
            response.send(users);
        } else {
            response.status(400).send('Error in insert new record');
        }
    });
});

app.listen(3001, function () {
    console.log('Express server is listening on port 3000');
});
like image 163
Uladzislau Vavilau Avatar answered Oct 06 '22 11:10

Uladzislau Vavilau