React Native AWS Amplify listUsersInGroup仅在IOS上引发网络错误

kmb7vmvb  于 2023-08-25  发布在  React
关注(0)|答案(1)|浏览(130)

我正在使用Expo - react native构建一个应用程序。对于用户身份验证,我使用AWS amplify,所有用户都会根据lambda函数自动归入不同的组。当我尝试调用AWS AdminQueries '/listUsersInGroup'时,它在Android和Web上都能正常工作。但在IOS上,我遇到了一个网络错误。我正在使用react native 0.71运行expo 48.0.0。5在iPhone 14模拟器上。
任何关于进一步调试的帮助都将是非常棒的。当使用fetch API而不是AWS amplify API时,完全相同的请求在IOS上工作得很好。
这就是代码:

import React, { useState, useEffect } from 'react';
import { View, Text, Button, Pressable, StyleSheet } from 'react-native';
import {useNavigation} from '@react-navigation/native';

import { Amplify, Auth, API } from 'aws-amplify';
import awsconfig from '../../aws-exports';
Amplify.configure(awsconfig);

export default function EmployeesCultureplace(){

  const navigation = useNavigation();

  const [user, setUser] = useState()
  const [isReady, setIsReady] = useState(false);
  const [employees,setEmployees] = useState([])
  const [hasAnyUsers,setHasAnyUsers] = useState(null);

  const [tempText, setTempText] = useState("no employees")

  let prepare = async () => {
    try {
      let userData = JSON.parse(await AsyncStorage.getItem('user'))
      setUser(userData)
      setHasAnyUsers(null)
      setEmployees([])

      return userData
    } catch(e){
      console.log(e)
    } finally {
      setIsReady(true)
    } 
  }

  useEffect(() => {
    prepare().then((user) => {
      listUsers(user).then((e) => {
        setEmployees(user)
      }).catch((e) => {
        // here we catch the error
        console.log("ERROR")
        console.log(e)
        console.log(JSON.stringify(e))
      })
    })

  },[])
 
  // THIS IS THE FUNCTION that's giving alot of issues
  let nextToken;
  let listUsers = async (user) => {
    let apiName = 'AdminQueries';
    let path = '/listUsersInGroup';

    let myInit = { 
      queryStringParameters: {
        "groupname": [(user["custom:group"])]
      },
      headers: {
        'Content-Type' : 'application/json',
        Authorization: `${(await Auth.currentSession()).getAccessToken().getJwtToken()}`
      }
  }
  const { NextToken, ...rest } =  await API.get(apiName, path, myInit);
  nextToken = NextToken;
  return rest;
}
   

  return (
  <></>
    
  )
}

这是一个错误,只出现在IOS上:在短期内,错误写道[错误:网络错误],堆栈跟踪如下:

{
"message":"Network Error",
"name":"Error",
"stack":"Error: Network Error\n    at createError (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:238728:26)\n    
at handleError (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:238650:27)\n    at call (native)\n    
at dispatchEvent (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:31717:31)\n    
at setReadyState (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30442:33)\n    
at __didCompleteResponse (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30253:29)\n    
at apply (native)\n    
at anonymous (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:30378:52)\n    
at apply (native)\n    
at emit (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2278:40)\n    
at apply (native)\n    
at __callFunction (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2812:36)\n    
at anonymous (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2573:31)\n    
at __guard (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2763:15)\n    
at callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=dk.project.sub:2572:21)",
"config":
{"transitional":{
"silentJSONParsing":true,
"forcedJSONParsing":true,
"clarifyTimeoutError":false
},
"transformRequest":[null],
"transformResponse":[null],
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1,
"maxBodyLength":-1,
"headers":{
"Accept":"application/json, text/plain, */*",
"User-Agent":"aws-amplify/5.1.5 react-native",
"Content-Type":"application/json",
"Authorization":"TOKENXXX",
"method":"get",
"url":"https://xxxxx.execute-api.eu-central-1.amazonaws.com/dev/listUsersInGroup?groupname=project",
"host":"xxxxxx.execute-api.eu-central-1.amazonaws.com",
"path":"/dev/listUsersInGroup",
"data":"null",
"responseType":"json",
"cancelToken":{"promise":{"_h":1,"_i":0,"_j":null,"_k":{"onRejected":null,"promise":{"_h":0,"_i":0,"_j":null,"_k":null}}},"_listeners":[null]}},"status":null}
7fyelxc5

7fyelxc51#

我刚刚遇到了同样的问题,解决方案是确保将头文件中的Content-Type设置为'Content-Type': 'Application/json'
由于某种原因,它被设置为'Content-Type': 'application/json'
我猜它在iOS上是大小写敏感的,但在Android上不是。

相关问题