skip to Main Content

I am creating a ticket scanner app with expo React Native.

When I scan a qr code I have a var called ScannedCode.

When I render the jsonData in the app the output is:

{"txid":"ch_3LZwATA4WCavBwXA1iN5diTN"}{"txid":"ch_3LZwBNA4WCavBwXA0kPBItB3"}{"txid":"ch_3LZwEkA4WCavBwXA02WfrvTP"}{"txid":"ch_3LZwFMA4WCavBwXA0xaSxBTv"}

I use an if statement to check if the ticket (txid) is valid.
So I want it to be something like this:

if (ScannedCode === jsonData) {Ticket is valid}
else {not valid] My problem is the if statment dont check each json data.

I want it to check if "one of the" json txid exist {ticket is valid}

In my code below I believe the IF statement check if the scanned code is equal to the total json data.
Because in the screenshot of the app, I scanned a code that existed in the jsonData, but the output was a red button that said not valid. It should be green and say valid.

Any suggestion?

  var TicketStatus= "No status"
  var StatusColor = "#000000"
  var ScannedCode = ScannedWithCamera;
//var jsonData= "ch_3LZwFMA4WCavBwXA0xaSxBTv";
  
    var jsonData = data.map((txid) => {

    return (
      JSON.stringify({ ...txid})
    )
  })



  if (ScannedCode === jsonData) {
    TicketStatus = "VALID"
    StatusColor = "green"
  } else if (ScannedCode === "") {
    TicketStatus = ""
  } else {
    TicketStatus = "NOT VALID"
    StatusColor = "RED"
  }

JSON-data from website

Screenshot of the App

2

Answers


  1. Chosen as BEST ANSWER

    Solved it like this:

    if (data.some(function(e){ return e.txid=== ScannedCode})) {
        BillettStatus = "GYLDIG"
        StatusColor = "#008000"
      } else if (ScannedCode === "") {
        BillettStatus = ""
      } else {
        BillettStatus = "IKKE GYLDIG"
        StatusColor = "#FF0000"
      }
    

  2. ‘jsonData’ receives an Array because the map always returns an array.

    you can fetch txid like this:

    const jsonScannedCode = jsonData[0]?.txid
    //jsonScannedCode will have id or it will be undefined
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search