keyv iterator not a fucntion error in npmjs keyv library

95 views Asked by At

I'm using https://www.npmjs.com/package/keyv

I'm able to access individual items using key.get, but Im unable to iterator through all items. In the below piece of code, test is printed correctly, but im unable to access keyv.iterator.

async function testReadDB()
{
  let keyv = new Keyv('sqlite://test.sqlite');
  test = await keyv.get("test_key")
  console.log(test)

  try {
    for (const [key, value] of keyv.iterator()) {
      console.log(key, value);
    };
  }
  catch (e) {
    console.log(e.message)
  }
}

Error: keyv.iterator is not a function or its return value is not iterable

1

There are 1 answers

0
sukru On

Since keyv.iterator() returns an AsyncIterator, it must be preceded by await:

async function testReadDB()
{
  let keyv = new Keyv('sqlite://test.sqlite');
  test = await keyv.get("test_key")
  console.log(test)

  try {
    // it must be preceded by "await"
    for await (const [key, value] of keyv.iterator()) {
      console.log(key, value);
    };
  }
  catch (e) {
    console.log(e.message)
  }
}