title: How do I reject writing value to Firebase if have the same value in my DB? (Javascript)
body: <p>I'm creating my custom order id with auto-increment generator function for my project. I will state my question here, if you want to know the whole story please read below.</p>
  
  <p>As written in the title, I need a way to reject my <a href="https://firebase.google.com/docs/reference/js/firebase.database.Reference#set" rel="nofollow noreferrer">set</a> to Firebase and it has to be done in 1 query. Currently, it will write my orderID to Firebase without rejecting it. But I need to reject if there is the same ID in the table.</p>
  
  <p>The short version of my code will be posted here, the whole function will be posted below.</p>
  
  <pre><code>  firebase.database().ref('orderCounter/orderIDsChecker/'+orderID).set({
      id: orderID,
    }, function(error) {
      if (error) {
        console.log('Order ID fail to generate. Regenerating new ID')
        createOrderID(orderCounterRef);
      } else {
        console.log('Order ID created!')  
      }
    });
  }
  </code></pre>
  
  <p>The story,</p>
  
  <p>I'm creating my own custom order id with auto-increment generator function for my project. The problem is that if multiple users creating order at the same time, it will generate the same id. Yes, I can use <a href="https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction" rel="nofollow noreferrer">transaction()</a> to solve the problem but I have no idea how to use it. Therefore, I have created my own version of the "transaction". With my method, I am able to prevent duplicates id unless 2 or more users create order within 1 second of gap. Or if anyone is kind enough to show me an example of how to write a transaction for my function, I thank you in advance. </p>
  
  <p>The flow of the code is, </p>
  
  <p>Get "currentMonth" and "orderIdCounter" from Firebase -> orderIdCounter +1 and update to Firebase -> start the process of generating order id -> Send the generated id to firebase -> If return success "order ID created", If not "got duplicate id" Re-run the whole process.</p>
  
  <p>Below is the code for my order id generator function. </p>
  
  <pre><code>function createOrderID(orderCounterRef){
    var childData = [];
    var orderID;
  
    //Get the Current Month and Order ID Counter from Firebase
    orderCounterRef.on('value', function(snap) { childData = snapshotToArrayWithoutID(snap); });
  
    var currentMonth = childData[0];
    var orderIDCounter = childData[1];
  
    if (orderIDCounter !== undefined){
      //Update orderIDCounter on Firebase. 
      //This is to prevent duplicate orderID when multiple users is creating order at the same time.
      var IDCounter = parseInt(orderIDCounter) + 1;
      //Set IDCounter to 3 digits
      IDCounter = ('00' + IDCounter.toString()).slice(-3);
      firebase.database().ref('orderCounter/orderIDCounter').set(IDCounter);
  
      //Handle the process to generate Order ID. Return in YYMMxxx(auto increment) format.
      orderID = handleCreateOrderID(currentMonth, (parseInt(orderIDCounter) - 1));
  
      //Check if duplicate ID on firebase
      firebase.database().ref('orderCounter/orderIDsChecker/'+orderID).set({
        id: orderID,
      }, function(error) {
        if (error) {
          console.log('Order ID fail to generate. Regenerating new ID')
          createOrderID(orderCounterRef);
        } else {
          console.log('Order ID created!')  
        }
      });
    }
    return orderID;
  }
  </code></pre>
  
  <p>My DB:</p>
  
  <p><img src="https://i.stack.imgur.com/lR3gy.png" alt="DB Screenshot"></p>