Retrieve a list of keys using the storage API

Hello, I would like to retrieve a list of keys that start with the word ‘level’ using the .query method of the storage API, and I encountered the following error:
ERROR 2023-08-08T01:39:05.963Z 9e446514-f7e8-41da-acda-cfb0dd195449 resolvedData.map is not a function
TypeError: resolvedData.map is not a function
at getData (index.js:39779:39)
at async index.js:33785:21
at async asyncMap (index.js:33716:24)
at async index.js:33737:29
at async index.js:33262:31
Here is my code:

 const getData = async () => {
    const {resolvedData} = await storage.query()
      .where('key',startsWith('level'))
      .limit(20)
      .getMany();
  
    const dataPromises = resolvedData.map((r) => r.value);
         for( let l of dataPromises){
    dataPromises.push(storage.get(l));
  }

  
    const data = await Promise.all(dataPromises);
  
    return data;
  };

On this line

const {resolvedData} = await storage.query()
      .where('key',startsWith('level'))
      .limit(20)
      .getMany();

You’re destructuring a field, resolvedData, that does not exist according to the docs. getMany returns a ListResult. See the docs. If you refactor the name of the variable resolvedDate to results it should get you past this problem. results is the name of the field on the object that contains the array of returned objects

2 Likes

@ryan is spot on regarding destructuring.

To add to that, in this snippet,

If you are trying to get the keys to be used in storage.get inside the for loop, perhaps are you looking to use r.key instead of r.value?

Cheers,
Ian

1 Like