Connect to the database via browser console

Asked

Viewed 88 times

1

I have a JS application that runs via browser console. I would like to know the possibilities of sending and receiving data via mysql via the Console.

I don’t know if it’s possible either.

I tried something, but it didn’t work. Example:

lastMsg = "Message received";   // recebe a msg
var mysql = require('mysql'); // 

var con = mysql.createConnection({
    host: "localhost",
    user: "yourusername",
    password: "yourpassword",
    database: "mydb"
});

con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
    var sql = "INSERT INTO mensagens (textomsg) VALUES (lastMsg)";
    con.query(sql, function (err, result) {
      if (err) throw err;
      console.log("1 record inserted");
    });
});

I get the following error on the console

Uncaught Referenceerror: require is not defined

  • 2

    Browser console + mysql will not work, you can use localStorage if you want to run in the browser or an api for the server.

  • Which path to an API?

  • I don’t understand the question

  • What is the best way to make an API in this case per console

  • You do not make an API in the browser console, it will be a server that provides the information, hence through the interface you can make the requests for the API, anyway, there is no single/simple way to do this.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.