Check the Result of a MySQL Query

Hello,
as you can see on the picture, make a MySQL query to it can come to the following states:

  1. query to DB => the "chipnr" is not present, then I get [empty].
  2. query to DB => the "chipnr" is available and I get the value in "payload[0].chipnr".
  3. output from DB => The entry was successful and I get msg.payload: OkPacket "[object Object]"

How do I have to set the switch node to get the output like this:

  1. state => value 1
  2. state => value 2
  3. state => value 3

Thanks

I'm not sure if this is what you are asking, but you could change your SELECT query to
select count(*) as recordcount from benutzer_hi_ma where chipnr = 20000123
Then the first two cases have msg.payload[0].recordcount == 0 or 1

I think that for the INSERT, msg.payload.affectedRows == 1 to indicate success, or else an error not a msg.payload.

@jbudd Thanks for the tip.

I have now solved it as follows:
I swapped the SQL query with the select count(*).
Then I replaced the switch with a function node and put a "Convert to JavaScript-Object" in front of the function and built the function like this:

> // define topic
> msg.topic = "RMChipScan";
> 
> // if insert success
> if (msg.payload.affectedRows == 1) {
>     msg.payload = "entered";
> }
> // if chipnr isnt available
> else if (msg.payload[0]["count(chipnr)"] == 0) {
>     msg.payload = "does not match";
> }
> // if chipnr is available
> else if (msg.payload[0]["count(chipnr)"] == 1) {
>     msg.payload = "matches";
> }
> 
> return msg;

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.