How to make the wix database items unique

In my Wix website, I created a database and the field named “giaCode” should be unique.

The problem is the Wix database doesn’t have any function to keep the items which are unique – like the foreign key.

So I decided to make a function to search and delete any duplicate items in the database. But it doesn’t work.

The process should be:

  1. Import the CSV file to the database
  2. Check all rows and record it’s “_id” to the array
  3. call the array to remove the rows

The Wix code (data.js) is below:

import wixData from 'wix-data';

let n,i,j,duplicateData,items;
let k = 0;

wixData.query("Diamond")
  .count()
  .then( (num) => {
    n = num;
});

wixData.query("Customer")
  .find()
  .then( (data) => {
    items = data.items;
 });

export function checkDuplicates(value, info) {
    for (i = 0; i < n; i++) { 
        for (j = 0; i < n; j++) { 
            wixData.query("myCollection")
            .eq("giaCode", items[i].giaCode)
            .find()
            .then ((DupResults) => {
                duplicateData[k][j] = DupResults.items;
                k++;
            });
        }
    }

    for (i = 0; i < k; i++) { 
        for (j = 1; j < duplicateData[k][j].giaCode.length; j++) {
            wixData.remove("Diamond", duplicateData[i][j].giaCode)
        }
    }
}


export function Diamond_afterInsert(item, context) {
    checkDuplicates()
}

It should be work but I get the error message:
enter image description here

35 thoughts on “How to make the wix database items unique”

  1. There are a few things you’re doing wrong if I understand you correctly. That is, if you are trying to stop duplicates from being added to the collection.

    1. Use a beforeInsert hook so the item never gets into the collection instead of deleting it once it is in there already.
    2. In your delete, you would have deleted the existing item with the giaCode along with the duplicate. I don’t think that is what you wanted.
    3. You are not call .eq() correctly. You aren’t saying what value it should be equal to.

    Try this instead:

    import wixData from 'wix-data';
    
    export function searchForDuplicates(value) {
      return wixData.query("Diamond")
        .eq("giaCode", value)
        .find()
        .then((results) => {
          return results.items.length;
        })
        .catch((err) => {
          let errorMsg = err;
        });
    }
    
    export function Diamond_beforeInsert(item) {
      return searchForDuplicates(item.giaCode).then((res) => {
        if(res > 0) {   
          return Promise.reject("Duplicate");
        }
        return item;
      });
    }
    

    I switched it to a beforeInsert hook, fixed the .eq(), and removed all the unnecessary code.

    Reply
  2. positive ana and breast cancer socialism , community definition biology gcse. positive pregnancy test then negative buy Pregabalin buy pregabalin 75 mg , friends yeti community action inc of central texas followers facebook , community bank laurel ms environments crossword clue? community action agency hartsville sc community college katy, community interactions biology quizlet community action gadsden al.
    community action research , community colleges near usc community college vs university social zone.

    Reply

Leave a Comment