Итак, вот мой вопрос, я хочу передать параметры в curl
следующим образом:
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" localhost:8000/api/addBrand -d '{"title":"radio","description":"radio","country":"Maroc","websiteurl":"radio.ma","livestream":"radio.ma/livestream"}'
Мой код:
const express = require('express');
const mariadb = require('mariadb/callback');
const db = mariadb.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'nodemysql'
});
db.connect((err) => {
if(err){
throw err;
}
else{
console.log('MariaDB Connected...');
}
});
const app = express();
app.get('/api/createdb', (req, res) => {
let sql = 'CREATE DATABASE nodemysql';
db.query(sql, (err, result) => {
if(err) throw err;
console.log(result);
res.send('Database created...');
});
});
// Insert brand
app.post('/api/addBrand', (req, res) =>{
var data = {
title : req.params.title,
description : req.params.description,
country : req.params.country,
websiteurl : req.params.websiteurl,
livestream : req.params.livestream
}
var params =[data.title, data.description, data.country, data.websiteurl, data.livestream]
let sql = 'INSERT INTO Brand(title, description, country, websiteurl, livestream) VALUES(?,?,?,?,?)'
let query = db.query(sql, params, (err, result) => {
if(err) throw err;
console.log(result);
res.send('Brand added...');
});
});
app.listen('8000', () => {
console.log('Server started on port 8000');
});
но у меня есть эта ошибка:
SqlError: Parameter at position 1 is undefined
sql: INSERT INTO Brand(title, description, country, websiteurl, livestream) VALUES(?,?,?,?,?) - parameters:[undefined,undefined,undefined,undefined,undefined]
at Object.module.exports.createError (/home/admin/node_modules/mariadb/lib/misc/errors.js:55:10)
at Query.throwNewError (/home/admin/node_modules/mariadb/lib/cmd/command.js:60:16)
at Query.validateParameters (/home/admin/node_modules/mariadb/lib/cmd/query.js:182:14)
at Query.start (/home/admin/node_modules/mariadb/lib/cmd/query.js:53:17)
at _addCommandEnablePipeline (/home/admin/node_modules/mariadb/lib/connection.js:1157:11)
at ConnectionCallback.Connection._queryCallback (/home/admin/node_modules/mariadb/lib/connection.js:532:5)
at /home/admin/test.js:53:21
at Layer.handle [as handle_request] (/home/admin/node_modules/express/lib/router/layer.js:95:5)
at next (/home/admin/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/admin/node_modules/express/lib/router/route.js:112:3) {
fatal: false,
errno: 45017,
sqlState: 'HY000',
code: 'ER_PARAMETER_UNDEFINED'
Может кто-нибудь мне помочь? Я застрял. Спасибо!