“错误:以错误的顺序接收到的分组."连接到无服务器极光时 [英] "Error: Received packet in the wrong sequence." when connect to serverless aurora

查看:290
本文介绍了“错误:以错误的顺序接收到的分组."连接到无服务器极光时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个Web应用程序,它调用lambda函数从数据库中获取数据.
我选择了无服务器Aurora并编写了代码,但是在查询方法中出现异常"Error: Received packet in the wrong sequence.".

I'm implementing a web application and it calls lambda function to get data from database.
I chose Serverless Aurora and wrote a code, but I get the exception "Error: Received packet in the wrong sequence." in query method.

我用谷歌搜索了这个问题,但是几乎所有的都太老了.
一篇文章说这是browisify的问题,但我不使用它.
我正在使用带打字稿的无服务器框架.

I googled this issue but almost of all is too old.
An article said it is the problem of browisify but I don't use it.
I'm using serverless framework with typescript.

const mysql = require('serverless-mysql')({
  config: {
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD
  }
});

export async function query(sql: string, param?: Array<string>): Promise<any> {
  const results = await mysql.query(sql).catch(e => {
    console.log(e); // Error: Received packet in the wrong sequence
    throw new Error(e);
  });

  await mysql.end();
  return results;
}

也无法正常工作


export async function query(sql: string, param?: Array<string>): Promise<any> {
  const connQueryPromisified = util
    .promisify(connection.query)
    .bind(connection);
  const result = await connQueryPromisified(sql, param)
    .then(row => {
      console.log(row);
      return row;
    })
    .catch(err => {
      console.log(err); // getting Error: Received packet in the wrong sequence
      throw err;
    });
  return result;
}

我也尝试使用RDS DATA服务,但在我所在的地区不可用.

I also tried to use RDS DATA Service but in my region it is not available.

export async function query(sql: string, param?: Array<string>): Promise<any> {
 const params: aws.RDSDataService.Types.ExecuteSqlRequest = {
    awsSecretStoreArn: '***',
    dbClusterOrInstanceArn: '***',
    database: '***',
    schema: '***',
    sqlStatements: sql
  };
  console.log(params);
  try {
    const rdsService = new aws.RDSDataService({
      apiVersion: '2018-08-01',
      region: 'ap-northeast-1'
    });
    return rdsService
      .executeSql(params)
      .promise()
      .then(d => {
        return d;
      })
      .catch(e => {
        throw new Error(e);
      });
  } catch (err) {
    console.log(err); // nothing to say
    throw new Error(err);
  }
}

这是我的配置:
webpack.config.js

And here is my configurations:
webpack.config.js

const path = require('path');
const slsw = require('serverless-webpack');

module.exports = {
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /\.tsx?$/, loader: 'ts-loader' },
    ],
  },
};

tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es2017"
    ],
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es2017",
    "outDir": "lib"
  },
  "exclude": [
    "node_modules"
  ]
}

我只想查询从无服务器Aurora获得的记录.
有人可以帮我吗?

I want to query only getting records from Serverless Aurora.
Can anybody help me?

推荐答案

发生这种情况的原因是因为Webpack(处于生产模式)通过最小化器放置了代码,并且serverless-mysql正在使用的mysql模块与最小化不兼容.

The reason this is happening is because Webpack (in production mode) is putting your code through a minimiser, and the mysql module that serverless-mysql is using is not compatible with minimising.

您可以在这里看到问题: https://github.com/mysqljs/mysql/第1655期.依靠功能名称进行代码构建的Node模块是一个普遍的问题,因为丑陋的/缩小者试图通过将功能名称更改为单个字母来混淆/节省空间.

You can see the issue here: https://github.com/mysqljs/mysql/issues/1655. It's quite a common problem with Node modules which rely on function names to do code building, as uglyifiers/minifiers attempt to obfuscate/save space by changing the names of functions to single letters.

最简单的解决方法是通过添加以下内容来关闭Webpack配置中的最小化:

The simplest fix would be to switch off minimising in your webpack config by adding:

  optimization: {
    minimize: false
  }

链接的问题中有一些讨论,涉及将其他各种最小化插件(如terser)配置为 not 杂名,这将使您在需要时获得一些最小化的好处.

There is some discussion in the linked issue on configuring various other minimising plugins (like terser) to not mangle names, which would allow you to get some of the benefit of minimising if you need it.

这篇关于“错误:以错误的顺序接收到的分组."连接到无服务器极光时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆