0

Im trying to create a chrome extension, and have encountered an annoying problem. I have this function, where I add an object to my indexedDB if the urlKeyValue of that object, does not already exist in the database.

If the urlKeyValue does exist in the DB, i want to get the object in the DB that contains that value. This is where my problem begins. On line 184 - 188, i try to get the object by searching by the `urlKeyValue, but when I try to print the object, i get undefined.

Question: How to get an object from IndexedDB by a value?

This is how my objects are structured:

    {
    message: "insert",

    payload: [
      {
        urlKeyValue: "",
        url: {
          [userId]: {
            [time]: {
              msg: form_data.get("message"),
            },
          },
        },
      },
    ],
  }

My Function where it all happens:

function insert_records(records, when) {
  if (db) {
    const insert_transaction = db.transaction(["roster2"], "readwrite");
    const objectStore = insert_transaction.objectStore("roster2");

    return new Promise((resolve, reject) => {
      insert_transaction.oncomplete = function () {
        console.log("ALL INSERT TRANSACTIONS COMPLETE.");
        resolve(true);
      };

      insert_transaction.onerror = function () {
        console.log("PROBLEM INSERTING RECORDS.");
        resolve(false);
      };

      records.forEach((person) => {
        // the "when" varieable isnt important, just disregard it
        if (when != "init") {
          const index = objectStore.index("urlKeyValue");
          let search = index.get(person.urlKeyValue);

          search.onsuccess = function (event) {
            if (search.result === undefined) {
              // no record with that key
              let request = objectStore.add(person);

              request.onsuccess = function () {
                console.log("Added: ", person);
              };
            } 
            else {
              const request2 = objectStore.get(person.urlKeyValue);

              request2.onsuccess = function (event) {
                console.log("--------" +  event.target.result);
              };

            }
          };
        } else {
          // this else statement isnt really important for this question 
          let request = objectStore.add(person);

          request.onsuccess = function () {
            console.log("Added: ", person);
            //self.location.href;
          };
        }
      });
    });
  }
}

EDIT:

This is an example of an object I store:

let roster = [
  {
    urlKeyValue: "https://www.youtube.com/",
    "https://www.youtube.com/": {
      1: {
        20: {
          msg: "this is some init data",
        },
      },
    },
  },
];
Nick
  • 219
  • 4
  • 15
  • I'm not a master in indexedDB, but I banged my head against the wall in the past. If I understant well your problem the solution is "no solution sorry, only workaround though". You can query your db by primary key or index key path. If it's crucial for you enagage a performante query on that value you have to define a new index (simple or compound) on that value. Alternatively you can fetch more than you need (at most the whole objectStore) and the filter the result. – Robbi Sep 24 '22 at 20:51
  • Remove request2 and simply use `search.result`, it already has the value you need. Also maybe find a wrapper for indexeddb and use await/async syntax. – wOxxOm Sep 24 '22 at 21:18
  • When i use `search.result` I just get `[object Object]` which is better than **undefined** but still not what I want. Any ideas on how to actually get the values, and the Url object? @wOxxOm – Nick Sep 25 '22 at 18:11
  • In JavaScript it means that you concatenated it with a string either when you saved it earlier or when you printed it in console.log. If you didn't do it, then it must mean you're looking at chrome://extensions Errors page. Don't use it. Use devtools for the page you're debugging. Either way, the solution doesn't change. – wOxxOm Sep 25 '22 at 18:39
  • I dont know what that means. What does **concatenated it with a string** mean? – Nick Sep 25 '22 at 18:42
  • `'foo' + object` – wOxxOm Sep 25 '22 at 18:42
  • I dont think I have done that. I have made an edit to my post. That is the object I store. Am I doing something wrong there? – Nick Sep 25 '22 at 18:49

0 Answers0