0

I've a table in mysql that i need to be populated by calling a webservice.

The catch is, I would like to pass the data to be inserted into the database as parameters to the webservice.

How would I do this?

1 Answer 1

1

Using the MySQL module

http://npmjs.com/mysql

const mysql = require('mysql');
const express = require('express');
const app = express();
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'me',
    password: 'secret',
    database: 'my_db'
});

connection.connect();

app.get('/', (req, res) => {
    connection.query(`SELECT * FROM table`, (error, data) => {
        if(error) {
            console.log(error)
        }else{
            res.json(data)
        } 
    });
});

app.post('/update/user/:id?', (req, res) => {
    const id = req.params.id;
    const username = req.body.username;
    connection.query(`UPDATE MyGuests SET username='${username}' WHERE id=${id}`, (error, data) => {
        if(error) {
            console.log(error)
        }else{
            res.json(data)
        } 
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I understand that I should use the MYSQL module. how do i pass parameters to insert data into a table to the js file?
yes, thank you, however i noticed a minor issue .. => is not necessary between (error) & {.
Oh ya, silly me, why the hey I'll put a fat array in a if statesmen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.