Akbar Fardi Ответов: 1

Как подключить базу данных msyql к node.js?


у меня есть база данных в mysql с именем address_book и таблицей karbar.
я пишу этот код для подключения :



и моя ошибка в cmd такова :
C:\Users\akbar\Desktop\CODE\node.js\http server>node server.js
Server is running..
{ ConnectionError: Login failed for user 'akbar'.
    at Connection.<anonymous> (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\mssql\lib\tedious.js:378:25)
    at Connection.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at Connection.processLogin7Response (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:672:16)
    at Connection.message (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:1082:21)
    at Connection.dispatchEvent (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:519:45)
    at MessageIO.<anonymous> (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:439:23)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:185:7)
  name: 'ConnectionError',
  message: 'Login failed for user \'akbar\'.',
  code: 'ELOGIN' }
{ ConnectionError: Connection is closed.
    at C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\mssql\lib\main.js:1569:17
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'ConnectionError',
  message: 'Connection is closed.',
  code: 'ECONNCLOSED' }


Что я уже пробовал:

var express = require('express');
var app = express();

app.get('/', function (req, res) {

    var sql = require("mssql");

    // config for your database
    var config = {
        user: 'akbar',
        password: '',
        server: 'localhost',
        database: 'address_book'

}
    // connect to your database
    sql.connect(config, function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from dbo.karbar', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});

var server = app.listen(3000, function () {
    console.log('Server is running..');
});

1 Ответов

Рейтинг:
2

Peter Leow

Вы упомянули MySQL, но ваш код предназначен для MSSQL. Сделать свой ум:
1. Node.js и учебник по MySQL / Codeforgeek[^]
2. Доступ к SQL Server в Node.js[^]


Akbar Fardi

я маэн в MSSQL