var result = await someFunc() returns an object but I expected list of object

hiropon

I wrote following function for loading indexeddb. (from IndexedDB 備忘メモ) I think this function should return Array of object. But, sometimes it returns an object. What are the possibilities of bug ? Chrome developer tool said type of object was Array during in "load" function. But, after received "records" is type of object.

    async function load(dbobj, db, index, range) {
      return new Promise(async (resolve, reject) => {
        const saves = [];
        const req = db.transaction(dbobj.storeName).objectStore(dbobj.storeName).index(index).openCursor(range);
        const onfinished = () => {
          console.log(`${saves.length} saves found.`);
          if (saves.length > 0) {
            resolve(saves[saves.length - 1]);
          }
        };
        req.onerror = reject;
        req.onsuccess = (ev) => {
          const cur = ev.target.result;
          if (cur) {
            saves.push(cur.value);
            cur.continue();
          } else {
            onfinished();
          }
        };
      });
    }

    // example of receiving data
    var records = await load(dbobj, db, index, range);
Dhananjai Pai

you are resolving only the value at the last index! resolve(saves) if you need the entire array;

async function load(dbobj, db, index, range) {
      return new Promise(async (resolve, reject) => {
        const saves = [];
        const req = db.transaction(dbobj.storeName).objectStore(dbobj.storeName).index(index).openCursor(range);
        const onfinished = () => {
          console.log(`${saves.length} saves found.`);
          if (saves.length > 0) {
            resolve(saves); // you are resolving only the value at the last index! resolve(saves) if you need the entire array;
          }
        };
        req.onerror = reject;
        req.onsuccess = (ev) => {
          const cur = ev.target.result;
          if (cur) {
            saves.push(cur.value);
            cur.continue();
          } else {
            onfinished();
          }
        };
      });
    }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Setting an empty list of type object to a function that returns IEnumerable of type object

분류에서Dev

foreach list <object> in object

분류에서Dev

For returns 1 object Angularjs

분류에서Dev

I have JSON Object, with this object i need to find the ID Number of "xxxEmployeeCategory "T" and list the list in an Array

분류에서Dev

Object[] inside Object[] Java returns memory address?

분류에서Dev

$resource returns Object with numeric attributes

분류에서Dev

Angular Select List Binding an Object when I was expecting a Value

분류에서Dev

Not getting list of parse object I am fetching from parse

분류에서Dev

Javascript - Getting propriety from object in var on for loop

분류에서Dev

List Object with multiple "keys" in it

분류에서Dev

How to manipulate LIst<Object>

분류에서Dev

List <Object> 정렬

분류에서Dev

Expected argument of type "object or array", "integer" given

분류에서Dev

CheckedListBox and List of Object's list

분류에서Dev

녹아웃을 사용하여 <object ...> <param name = "someName"value = "$ root.someFunc"/> </ object> 바인딩을 달성하려면 어떻게해야합니까?

분류에서Dev

sort List of Object by one of Object's field

분류에서Dev

sort List of Object by one of Object's field

분류에서Dev

Reading values into a List object - Object reference not set to an instance of an object

분류에서Dev

why adding date object and a time object returns an incorrect value?

분류에서Dev

Flex App Sqlite Select statement returns [object Object]

분류에서Dev

Powershell Where-Object excluding a result

분류에서Dev

JQuery : Get a value from the result object array

분류에서Dev

Dapper > I cannot retrieve object with child object

분류에서Dev

AWS Cognito AdminGetUser returns undefined object

분류에서Dev

Object function returns function instead of value

분류에서Dev

Value from JavaScript object returns null

분류에서Dev

$.post returns object when aiming for simple php

분류에서Dev

Merge two list object in dart

분류에서Dev

Deserializing Xml string into object with List<>

Related 관련 기사

  1. 1

    Setting an empty list of type object to a function that returns IEnumerable of type object

  2. 2

    foreach list <object> in object

  3. 3

    For returns 1 object Angularjs

  4. 4

    I have JSON Object, with this object i need to find the ID Number of "xxxEmployeeCategory "T" and list the list in an Array

  5. 5

    Object[] inside Object[] Java returns memory address?

  6. 6

    $resource returns Object with numeric attributes

  7. 7

    Angular Select List Binding an Object when I was expecting a Value

  8. 8

    Not getting list of parse object I am fetching from parse

  9. 9

    Javascript - Getting propriety from object in var on for loop

  10. 10

    List Object with multiple "keys" in it

  11. 11

    How to manipulate LIst<Object>

  12. 12

    List <Object> 정렬

  13. 13

    Expected argument of type "object or array", "integer" given

  14. 14

    CheckedListBox and List of Object's list

  15. 15

    녹아웃을 사용하여 <object ...> <param name = "someName"value = "$ root.someFunc"/> </ object> 바인딩을 달성하려면 어떻게해야합니까?

  16. 16

    sort List of Object by one of Object's field

  17. 17

    sort List of Object by one of Object's field

  18. 18

    Reading values into a List object - Object reference not set to an instance of an object

  19. 19

    why adding date object and a time object returns an incorrect value?

  20. 20

    Flex App Sqlite Select statement returns [object Object]

  21. 21

    Powershell Where-Object excluding a result

  22. 22

    JQuery : Get a value from the result object array

  23. 23

    Dapper > I cannot retrieve object with child object

  24. 24

    AWS Cognito AdminGetUser returns undefined object

  25. 25

    Object function returns function instead of value

  26. 26

    Value from JavaScript object returns null

  27. 27

    $.post returns object when aiming for simple php

  28. 28

    Merge two list object in dart

  29. 29

    Deserializing Xml string into object with List<>

뜨겁다태그

보관