Received packet in the wrong sequence

Hi,
*I was using a lambda function to connect to MySQL database code here

var connection = mysql.createConnection({
          host : "remotemysql.com",
          user: "",
          database: "",
          password: ""
          //port:"3306"
        });
    }

exports.handler = function (event, context, callback) {
    context.callbackWaitsForEmptyEventLoop =false;
    connection.connect(function(err) {
      if (err) {
        console.error('Error connecting: ' + err.stack);
      }
      console.log('Connected as thread id: ' + connection.threadId);
    });

var results=[];
//SQL Query > Select Data

return new Promise((resolve, reject) => {
  const readTable = `SELECT * FROM num_of_likes`;
  connection.query(readTable, (err, results, fields) => {
    if (err) {
     reject(err);
    } else {
      resolve({statusCode: 200, body: {results}});
    }
  });
});

*Lambda server is listening on 9000
Request from ::1: GET /connect
Error connecting: Error: Received packet in the wrong sequence.
at A._parsePacket (/Users/riteshkarwa/netlify/functions/connect.js:1:93387)
at A._parsePacket (/Users/riteshkarwa/netlify/functions/connect.js:1:102113)
at A.write (/Users/riteshkarwa/netlify/functions/connect.js:1:96341)
at A.write (/Users/riteshkarwa/netlify/functions/connect.js:1:89855)
at Socket. (/Users/riteshkarwa/netlify/functions/connect.js:1:7771)
at Socket. (/Users/riteshkarwa/netlify/functions/connect.js:1:6835)
at Socket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
--------------------
at A._enqueue (/Users/riteshkarwa/netlify/functions/connect.js:1:91233)
at A.handshake (/Users/riteshkarwa/netlify/functions/connect.js:1:89978)
at o.connect (/Users/riteshkarwa/netlify/functions/connect.js:1:8643)
at Object.E.handler (/Users/riteshkarwa/netlify/functions/connect.js:1:238235)
at /Users/riteshkarwa/netlify/node_modules/netlify-lambda/lib/serve.js:144:27
at Layer.handle [as handle_request] (/Users/riteshkarwa/netlify/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:137:13)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at Route.dispatch (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/riteshkarwa/netlify/node_modules/express/lib/router/layer.js:95:5)
at /Users/riteshkarwa/netlify/node_modules/express/lib/router/index.js:281:22
Connected as thread id: 15223054
Response with status 500 in 2105 ms.
Error during invocation: Error: Received packet in the wrong sequence.
at A._parsePacket (/Users/riteshkarwa/netlify/functions/connect.js:1:93387)
at A._parsePacket (/Users/riteshkarwa/netlify/functions/connect.js:1:102113)
at A.write (/Users/riteshkarwa/netlify/functions/connect.js:1:96341)
at A.write (/Users/riteshkarwa/netlify/functions/connect.js:1:89855)
at Socket. (/Users/riteshkarwa/netlify/functions/connect.js:1:7771)
at Socket. (/Users/riteshkarwa/netlify/functions/connect.js:1:6835)
at Socket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
--------------------
at A._enqueue (/Users/riteshkarwa/netlify/functions/connect.js:1:91233)
at A.handshake (/Users/riteshkarwa/netlify/functions/connect.js:1:89978)
at o.connect (/Users/riteshkarwa/netlify/functions/connect.js:1:8643)
at Object.E.handler (/Users/riteshkarwa/netlify/functions/connect.js:1:238235)
at /Users/riteshkarwa/netlify/node_modules/netlify-lambda/lib/serve.js:144:27
at Layer.handle [as handle_request] (/Users/riteshkarwa/netlify/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:137:13)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at next (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:131:14)
at Route.dispatch (/Users/riteshkarwa/netlify/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/riteshkarwa/netlify/node_modules/express/lib/router/layer.js:95:5)
at /Users/riteshkarwa/netlify/node_modules/express/lib/router/index.js:281:22 {
code: ‘PROTOCOL_INCORRECT_PACKET_SEQUENCE’,
fatal: true
}

so far I did not find a way to solve it by search google

Hi @ritesh, are you bundling your function? if so, try without bundling and se if it works for you.

hi @futuregerald , as far as I know I have not bundled the function, I just created connect.js which has the logic to connect to MySQL database. So i went ahead and disabled everything in asset optimization setting.
I am just experimenting, I don’t think I am doing it right.

Error messages i got after deploy and hitting https://southernsunshineandroses.netlify.com/.netlify/functions/connect
{"errorMessage":"Received packet in the wrong sequence.","errorType":"Error","stackTrace":["s._parsePacket (/var/task/connect.js:1:106224)","s._parsePacket (/var/task/connect.js:1:114950)","s.write (/var/task/connect.js:1:109178)","s.write (/var/task/connect.js:1:102692)","Socket.<anonymous> (/var/task/connect.js:1:9937)","Socket.<anonymous> (/var/task/connect.js:1:9001)","emitOne (events.js:116:13)","Socket.emit (events.js:211:7)","addChunk (_stream_readable.js:263:12)","readableAddChunk (_stream_readable.js:250:11)"," --------------------","s._enqueue (/var/task/connect.js:1:104070)","s.handshake (/var/task/connect.js:1:102815)","T.connect (/var/task/connect.js:1:10809)","e.handler (/var/task/connect.js:8:18859)"]}

OK, and this function works locally when run with netlify-lambda serve ?

Hi @fool , it does not work locally as well same error

ah, ok. That is extremely useful information to tell us up front in the future, please!

Our stiff can’t help consult about your code not working; just about working code that doesn’t work on netlify :slight_smile:

Other community members may have more advice for you though they may not too…

I have a very similar issue (using mysql and the netlify functions express demo app) to hit a DB on heroku (also tried on an AWS RDS instance)

I get the same error and I can’t figure out what is causing it. It will work locally but not when it is published.

I have seen a few threads here and there mention mysql config or import getting stepped on or removed during the build process. Something to do with webpack and where and how mysql is declared and configured. But nothing explaining what to do about it

i was not able to get to work locally as well, i was using freehostmysql

Hi, @Ju66ernaut. If it works locally but not at Netlify then the issue is most likely a difference in the function environments. The next step will be to determine what is present locally and not in the deployed function.

It often helps debugging to start logging verbosely in both places to see if the difference can be found that way.

For issues with code not working locally, we cannot assist with troubleshooting those as they are not Netlify specific.

Hi @luke thanks for the reply.

From what I have been reading it sounds like this issue may be related to how mysql is bundled/minified during the deploy process but after changing my netlify.toml file to not bundle or minify and it still not working I am not so sure. I also tried including a webpack.config.js file and set optimization.minimize: false.

Are there any demos or examples of connecting to mysql via a netlify function? Or is there a preferred DB to use with netlify functions? I tried the faunaDB tutorial but had some issues getting that up and running.

Lastly, for verbose logging in my function can you tell me anything in particular I should be looking for?
Also I just wanted to state again I am using the express demo app as a starter project and just modifying that.
Thank you

Hi, with regards to bundling/minified, are you using netlify-lambda or our buildbot’s built-in ‘zip-it-and-ship-it’ method? You may want to try not bundling at all.

With regards to examples, I don’t have one handy at the moment. As far as logging in express, it would be up to you to either console.log certain res or req objects or use a logger middleware.

Hi @Dennis I am working off the netlify-express demo and I see it is using netlify-lamda 1.6.3. I have been trying to either override bundling by adding my own web.config.js and setting optimization.minimize = false and I also tried disabled bundling and minification in the netlify admin UI (site settings) but that looks like it didn’t work either.

I am not aware of buildbot but I see the repo for it so I will give that a shot I think. Is there anything in particular I should know about using that?

Thanks for your help

Hi @Dennis,
the recommended solution mentioned here is https://www.bountysource.com/issues/42621816-nodejs-api-webpack-2-production-builds-error-received-packet-in-the-wrong-sequence
with uglify, is there the way to turn that off here is my package.json

       {
      "name": "netlify-express",
      "version": "0.0.1",
      "description": "SouthernSunshineRose website code",
      "main": "functions/server.js",
      "scripts": {
        "build": "netlify-lambda build express",
        "start": "nodemon local-server.js",
        "start:lambda": "netlify-lambda serve express"
      },
      "repository": {
        "type": "git",
        "url": "https://github.com/riteshkarwa/my-repository.git"
      },
      "keywords": [
        "netlify",
        "express.js"
      ],
      "author": "SouthernSunshineRose",
      "license": "Apache-2.0",
      "homepage": "https://github.com/riteshkarwa/my-repository.git",
      "dependencies": {
        "angular": "1.7.x",
        "angular-route": "1.7.x",
        "axios": "^0.19.0",
        "body-parser": "^1.19.0",
        "bootstrap": "^3.4.0",
        "cors": "^2.8.4",
        "express": "^4.17.1",
        "faunadb": "^2.10.0",
        "mysql": "^2.15.0",
        "netlify-lambda": "^1.6.3",
        "nodemon": "^1.19.2",
        "serverless-http": "^2.3.0"
      }
    }

Perhaps you can set those recommended solutions in the webpack configuration for netlify-lambda. How to do this is described here.