简体   繁体   English

尝试使用 Node.js MSSQL 连接到 SQL 服务器数据库时出现错误 ELIFECYCLE 3228369023

[英]Error ELIFECYCLE 3228369023 on attempted connection to SQL Server database using Node.js MSSQL

I'm working on setting up an API for a SQL Server Express database, I'm planning on using mssql in Node.js with express to receive requests and interact with the database.我正在为 SQL 服务器 Express 数据库设置 API,我计划在 Node.js 中使用 mssql 来接收请求并与数据库交互。 I have tried several approaches to connecting to the DB via node, and believe that the server (running locally) is found, but my node app is unable to connect with it.我已经尝试了几种通过节点连接到数据库的方法,并且相信找到了服务器(在本地运行),但是我的节点应用程序无法与其连接。

The approaches I've made include various different forms of connection strings (some just strings as displayed below, and some attempts using a JSON object).我采用的方法包括各种不同的 forms 连接字符串(有些只是如下所示的字符串,有些尝试使用 JSON 对象)。 Also, at first I only used the mssql package, but I later added in msnodesqlv8.还有,一开始我只用了mssql package,后来在msnodesqlv8中添加了。

function connect(){
var sql = require("mssql/msnodesqlv8");

// config for your database
var connString = 'Driver={SQL Server Native Client 11.0};Server={DESKTOP-RJH9ERF\\SQLEXPRESS};Uid={DESKTOP-RJH9ERF\\Jake};Database={MAIC};Trusted_Connection={yes};'

// connect to your database
sql.connect(connString, 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 Events', function (err, recordset) {

        if (err) console.log(err)

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

    });
});
};

At first, there appeared to be an issue with a user not being recognized.起初,似乎存在无法识别用户的问题。 On my more recent approaches to connecting, however, I am getting this error repeatedly.但是,在我最近的连接方法中,我反复收到此错误。

20 error code ELIFECYCLE 21 error errno 3228369023 22 error maic-api@1.0.0 start: node server.js 22 error Exit status 3228369023 20 错误代码 ELIFECYCLE 21 错误 errno 3228369023 22 错误 maic-api@1.0.0 开始: node server.js 22 错误退出状态 3228369023

I have seen no additional information regarding what this error means and after some searches online it remains unclear what is causing this.我没有看到有关此错误含义的其他信息,并且在网上进行了一些搜索后,仍不清楚是什么原因造成的。 Is anyone familiar with this error and what it might have to do with MSSQL in node?有没有人熟悉这个错误以及它可能与节点中的 MSSQL 有什么关系?

Sorry for being late to the party, I just fixed the same error right now.很抱歉迟到了,我现在刚刚修复了同样的错误。 What was happening on my side was that I was using a .env file to provide variables for the database connection configuration and since it is bad practice to commit your .env , I had it stored locally.我这边发生的事情是我使用.env文件为数据库连接配置提供变量,并且由于提交.env是不好的做法,我将它存储在本地。 When I switched to my laptop, the .env file was gone, and so were my DB_USER and DB_PASS environment variables (they were actually undefined ).当我切换到笔记本电脑时, .env文件消失了,我的DB_USERDB_PASS环境变量也消失了(它们实际上是undefined )。

For some reason, I could only make the tedious logs appear by placing console.log('OK') right after the require which was causing the error, like below.出于某种原因,我只能通过将console.log('OK')放在导致错误的require之后来显示tedious的日志,如下所示。 My guess is that for some reason the error buffer was not getting flushed.我的猜测是由于某种原因错误缓冲区没有被刷新。

db-conn.js db-conn.js

require('dotenv').config
var Connection = require('tedious').Connection

var config = {
  server: process.env.DB_SERVER,
  options: {
    database: process.env.DB_NAME,
    trustServerCertificate: true,
    encrypt: true,
  },
  authentication: {
    type: "default",
    options: {
      userName: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
    },
  },
};

const connection = new Connection(config);

module.exports = connection;

server.js服务器.js

const connection = require("./server/db-conn");
console.log('OK');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM