首页 > 解决方案 > 我在我的 React Native 应用程序中获取 Api 时遇到问题

问题描述

我已经在 react native 中构建了一个应用程序,所有其他 Api 都可以顺利获取,但是大多数时候我从这个 API 方法获取时都会出错。有时这会给出结果,但大多数时候它会进入错误块。当我在邮递员中测试时,我得到了结果。这是我的获取请求的代码块:-

 async pagination(collectionId, count) {
  if (!count) {
     count = 3;
   }
return new Promise((resolve, reject) => {
  fetch(AdminUrl, {
     method: 'POST',
     headers: {
               'Content-Type': 'application/graphql',
                Authorization: Authorization,
                // 'X-Shopify-Storefront-Access-Token': '088879868575'
              },

    body:
      `{
        collections(first: 1, query: "id:` +
        collectionId +
        `") {
     edges {
        node {
              id,
                title,
                products(first: ` +
               count +
                    `){
                  edges{
                     node{
                            id,
                               title,
                            description,
                            vendor,
                            tags
                        variants(first: 1) {
                            edges {
                                node {
                                    id
                                    title
                                    price
                                    sku
                                    position
                                    compareAtPrice
                                    barcode
                                    taxable
                                    weight
                                    weightUnit
                                    inventoryQuantity

                                }
                            }
                        }
                        images(first: 1) {
                            edges {
                                node {
                                    id
                                    src
                                }
                            }
                        }
                    }
                    cursor
                }

            }
        }
       }
     }
   } `,
  })
    .then(response => response.json())
    .then(responseJson => {
      resolve(responseJson);
    })
    .catch(error => {
      alert('error in fetching products by collection11111111111111111');
      console.log("error in product fetching - ", error)
      reject(error);
    });
});},

这是我收到的错误消息:-

         error in product fetching -  [SyntaxError: JSON Parse error: 
          Unrecognized token '<']
          WARN  Possible Unhandled Promise Rejection (id: 1):
          SyntaxError: JSON Parse error: Unrecognized token '<'
          parse@[native code]
          tryCallOne@http://10.0.2.2:8081/index.bundle? 
           platform=android&dev=true&minify=false:27011:16
           http://10.0.2.2:8081/index.bundle? 
           platform=android&dev=true&minify=false:27112:27
           _callTimer@http://10.0.2.2:8081/index.bundle? 
           platform=android&dev=true&minify=false:31138:17
          _callImmediatesPass@http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:31174:19
          callImmediates@http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:31393:33
          callImmediates@[native code]
          __callImmediates@http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:2632:35
          http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:2409:34
          __guard@http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:2615:15
          flushedQueue@http://10.0.2.2:8081/index.bundle? 
          platform=android&dev=true&minify=false:2408:21
         flushedQueue@[native code]
          invokeCallbackAndReturnFlushedQueue@[native code]

标签: javascriptreactjsreact-native

解决方案


推荐阅读