简体   繁体   English

node-mssql将不会执行提供unhandlePromiseRejectionWarning的存储过程

[英]node-mssql won't execute stored procedure giving unhandlePromiseRejectionWarning

The following is the output from my server.js file. 以下是我的server.js文件的输出。
I am printing out the query string parameters that I'm passing into the stored procedure. 我正在打印要传递给存储过程的查询字符串参数。

ID : 10################_
Email : test@mail.com
Name : testname
FamilyName : testtesttest

Below this is the error I'm getting: 下面是我得到的错误:

(node:8084) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'type' of undefined (节点:8084)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性'type'

(node:8084) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (节点:8084)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

Below is my call to the stored procedure. 以下是我对存储过程的调用。 I keep getting the above error and the stored procedure is not being executed. 我不断收到上述错误,并且未执行存储过程。 I've looked at the following link that appeared to have the same problem, but I'm still getting the same error. 我查看了以下链接 ,该链接似乎存在相同的问题,但仍然出现相同的错误。

This is my 1st attempt to call a stored procedure using this node module. 这是我第一次尝试使用此节点模块调用存储过程。 So any assistance is appreciated. 因此,感谢您的协助。

app.get('/addRegistration', function(req,res){
    try 
    {
        const pool5 = new sql2.ConnectionPool(config, err => 
            {
                // Stored Procedure
                pool5.request()
                .input('FBID', sql2.varchar, 250, req.query.id)
                .input('FBEmail', sql2.varchar, 250, req.query.email)
                .input('FBName', sql2.varchar, 250, req.query.memberName)
                .input('FamilyName', sql2.varchar, 250, req.query.familyName)
                .execute('sp_registerUser', (err, result) => 
                {
                    console.log("Success"); 
                }).catch(function(err) {
                    console.log(err);
                });
            })
         pool5.on('error', err => {
            console.log("Error");
         })
    } catch (err) {
    // ... error checks
        console.log("addRegistration: " + err)
    }       
});

抱歉,这听起来像是glib,但是我在您的代码中看不到您定义请求的位置-所以我最好的答案是错误消息未定义...

First create a connection file for the sql server. 首先为sql服务器创建一个连接文件。

import mysql from 'mysql';
import config from 'config-yml';
import logger from '../logger';

/**
 * @description Creating pool to have simultanious connection to SQL.
 */
const pool = mysql.createPool({
  connectionLimit: config.db.max_limit,
  host: config.db.host,
  user: config.db.user,
  port: config.db.port,
  password: config.db.password,
  database: config.db.name,
  debug: process.env.DB_DEBUG || false, // Set to true if you want to debug the database.
});

/**
 * @description Creates the database connection.
 */
pool.getConnection((err, connection) => {
  if (err) {
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
      logger.error('Database connection was closed.');
    }
    if (err.code === 'ER_CON_COUNT_ERROR') {
      logger.error('Database has too many connections.');
    }
    if (err.code === 'ECONNREFUSED') {
      logger.error('Database connection was refused.');
    }
  }
  if (connection) connection.release();
});

module.exports = pool;

Then create a export function to execute the query code follows. 然后创建一个导出函数来执行以下查询代码。

import pool from '../index';

const execQuery =  async (query, fn) => {
  pool.query(query, async (err, result, fields) => { // eslint-disable-line
    try {
      console.log(result[0][0]);
      fn(result[0][0]);
      return result;
    } catch (err) {
      return err;
    }
  });
};

export default execQuery;

After this write procedure in Workbench and call sql Procedure function 在Workbench中完成此写过程并调用sql Procedure函数

call SO_CALLED_PROCEDURE(PASS_ARGUMENT) ; call SO_CALLED_PROCEDURE(PASS_ARGUMENT) ;

it will work. 它会工作。

I found the issue. 我发现了问题。 It was my original code. 这是我的原始代码。

            .input('FBID', sql2.varchar, 250, req.query.id)
            .input('FBEmail', sql2.varchar, 250, req.query.email)
            .input('FBName', sql2.varchar, 250, req.query.memberName)
            .input('FamilyName', sql2.varchar, 250, req.query.familyName)

Needed to be changed to: 需要更改为:

            .input('FBID', sql2.VarChar(250), req.query.id)
            .input('FBEmail', sql2.VarChar(250), req.query.email)
            .input('FBName', sql2.VarChar(250), req.query.memberName)
            .input('FamilyName', sql2.VarChar(250), req.query.familyName)

Ultimately I had the size as part of the value parameter. 最终,我将尺寸作为值参数的一部分。 It was all wrong. 都错了 Once I did it correctly, then the stored procedure executed as expected. 一旦正确完成操作,存储过程就会按预期执行。

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

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