stackhero-io / node-red-contrib-stackhero-mysql

Node-RED node to read and write to a MySQL or a MariaDB database. Compatible with TLS (SSL) and "Caching SHA2 password" authentication method.
16 stars 7 forks source link

Fix object validation #3

Closed GrimbiXcode closed 3 years ago

GrimbiXcode commented 3 years ago

Description / reason:

The current check if the payload is an object leads to errors despite correct payload.

Improvement:

Way of checking if the payload is an object.

Bacto commented 3 years ago

Hi @GrimbiXcode, Many thanks for the PR!

I finally managed it with this function that seems a little more bulletproof: isAnObject = (value) => (value && typeof value === 'object' && (value.__proto__ == null || value.__proto__ === Object.prototype));

GrimbiXcode commented 3 years ago

Hi @Bacto, I have seen, thank you. Unfortunately this still does not work for me. My input from my function node:

msg.payload = {
  name: "Tester",
  age: 56,
};

After this, I added the following code above your function to get the reason of the error:

try {
  console.log(`-- ${typeof msg.payload === 'object'}`)
  console.log(`-- ${msg.payload.__proto__ === Object.prototype}`)
  console.log(`-- ${msg.payload.__proto__ == null}`)
  console.log(`-- ${!!msg.payload}`)
} catch (e) {
  console.log(`-EE- ${e}`)
}

.. and I got the following output:

-- true       // typeof msg.payload === 'object'            => makes sense 
-- false      // msg.payload.__proto__ === Object.prototype => that makes no sense to me
-- false      // msg.payload.__proto__ == null              => makes sense 
-- true       // !!msg.payload                              => makes sense 

Maybe you can do more with it than I can.

Bacto commented 3 years ago

Ok so it worked using the "inject" node but not using a function node. Thanks for the example! I've corrected it and published 1.0.5 :)

GrimbiXcode commented 3 years ago

Wow, that was fast. Thanks a lot!