web-dev-qa-db-fra.com

insérer un nouvel enregistrement dans nodejs en utilisant sequelize POST

Je veux insérer de nouvelles données dans la base de données en utilisant sequelize express, sans requête. J'essaie tellement mais je n'ai pas obtenu la sortie ... Si mon code est erroné, donnez-moi un code pour insérer un nouvel enregistrement dans db en utilisant la suite 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');
});
5
GaneSH

Vous devez utiliser body-pareser

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

Exemple d'utilisation de l'analyseur de corps:

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))
})

Exemple:

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 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');
});
4
Uladzislau Vavilau