javascript 如何在React应用程序中运行AWS而不出现调用尝试超出错误?

bxpogfeg  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(83)

我正在尝试构建一个React手机应用程序,该应用程序在启动后即可连接到AWS。第一次使用Expo运行该应用程序时,我收到了一个关于缺少模块“Util”的错误,因此我使用了这个单独的thread来解决该问题。之后,我收到了另一个关于“filesys.existsSync不是一个函数”的问题,因此我使用了另一个thread来解决该问题。
在第二个线程中,我通过WebSocket协议连接客户端,而不是使用证书。进行这些更改后,我测试了node.js文件中的连接是否正常,连接没有任何问题。我还可以确认React应用在没有AWS代码的Expo上运行没有错误。但是,当我尝试将两者结合起来时,我得到了以下error.
在Expo中,我得到了以下错误日志:

03:10
transform[stdout]: 
transform[stdout]: <--- Last few GCs --->
transform[stdout]: io[8441:0x9b1a90]    57166 ms: Mark-sweep 1387.9 (1429.3) -> 1386.5 (1430.8) MB, 3232.4 / 0.0 ms  (+ 145.9 ms in 119 steps since start of marking, biggest step 11.0 ms, walltime since start of marking 3448 ms) (average mu = 0.081, current mu = 0.022) alloca[8441:0x9b1a90]    60040 ms: Mark-sweep 1392.7 (1431.3) -> 1389.5 (1433.8) MB, 2729.3 / 0.0 ms  (+ 77.2 ms in 95 steps since start of marking, biggest step 10.8 ms, walltime since start of marking 2867 ms) (average mu = 0.056, current mu = 0.025) allocati
transform[stdout]: 
transform[stdout]: <--- JS stacktrace --->
transform[stdout]: 
transform[stdout]: ==== JS stack trace =========================================
transform[stdout]: 
transform[stdout]:     0: ExitFrame [pc: 0x74ec865452b]
transform[stdout]: Security context: 0x058c7e02ee11 <JSObject>
transform[stdout]:     1: /* anonymous */ [0xf6ddf9ef2c1] [/mnt/c/Users/aless/Desktop/UMNSVP/freya-app/new_branch/node_modules/@babel/traverse/lib/index.js:~75] [pc=0x74ec8b3ce6b](this=0x22bfe78ae6e9 <JSFunction traverse (sfi = 0x114f116c781)>,node=0x1467dbf20a49 <Node map = 0x1701e8edb8b1>,opts=0x1a7553733ca1 <Object map = 0x1b9c346b8861>,scope=0x3b66a58da481 <Scope map = 0x1b...
transform[stdout]: 
INFO
03:10
transform[stderr]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
INFO
03:10
transform[stderr]:  1: 0x7fcaeea2d46c node::Abort() [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  2: 0x7fcaeea2d4b5  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  3: 0x7fcaeec59e6a v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  4: 0x7fcaeec5a0e1 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  5: 0x7fcaeeff4c66  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  6: 0x7fcaef006043 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  7: 0x7fcaef006930 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  8: 0x7fcaef00891d v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]:  9: 0x7fcaef008975 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]: 10: 0x7fcaeefd4dda v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:10
transform[stderr]: 11: 0x7fcaef26031e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/lib/x86_64-linux-gnu/libnode.so.64]
transform[stderr]: 12: 0x74ec865452b 
INFO
03:11
transform[stdout]: 
transform[stdout]: <--- Last few GCs --->
transform[stdout]: 
transform[stdout]: [8496:0x16dfa90]    64912 ms: Scavenge 1383.0 (1422.8) -> 1382.7 (1423.3) MB, 7.6 / 0.0 ms  (average mu = 0.141, current mu = 0.013) allocation failure 
transform[stdout]: [8496:0x16dfa90]    67294 ms: Mark-sweep 1383.4 (1423.3) -> 1383.1 (1423.3) MB, 2377.6 / 0.0 ms  (average mu = 0.080, current mu = 0.007) allocation failure scavenge might not succeed
transform[stdout]: 
transform[stdout]: 
transform[stdout]: <--- JS stacktrace --->
transform[stdout]: 
transform[stdout]: ==== JS stack trace =========================================
transform[stdout]: 
transform[stdout]:     0: ExitFrame [pc: 0x276fc0c5452b]
transform[stdout]: Security context: 0x04399ea2ee11 <JSObject>
transform[stdout]:     1: visitQueue [0x343848f072f9] [/mnt/c/Users/aless/Desktop/UMNSVP/freya-app/new_branch/node_modules/@babel/traverse/lib/context.js:~94] [pc=0x276fc0f33dec](this=0x0d7bc0a5b659 <TraversalContext map = 0x97d5ee8c759>,queue=0x0d7bc0a5b6e9 <JSArray[1]>)
transform[stdout]:     2: /* anonymous */ [0x591909657f9] [/mnt/c/Users/aless/Desktop/UMNSVP/freya-app/new_branch/node_module...
transform[stdout]: 
INFO
03:11
transform[stderr]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
INFO
03:11
transform[stderr]:  1: 0x7fc5340fd46c node::Abort() [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  2: 0x7fc5340fd4b5  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  3: 0x7fc534329e6a v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  4: 0x7fc53432a0e1 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  5: 0x7fc5346c4c66  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  6: 0x7fc5346d6043 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  7: 0x7fc5346d6930 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  8: 0x7fc5346d891d v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]:  9: 0x7fc5346d8975 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]: 10: 0x7fc5346a4dda v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:11
transform[stderr]: 11: 0x7fc53493031e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/lib/x86_64-linux-gnu/libnode.so.64]
transform[stderr]: 12: 0x276fc0c5452b 
INFO
03:13
transform[stdout]: 
transform[stdout]: <--- Last few GCs --->
transform[stdout]: 
transform[stdout]: [8506:0x19f2a90]    65990 ms: Mark-sweep 1386.5 (1434.8) -> 1384.5 (1437.3) MB, 2579.9 / 0.0 ms  (average mu = 0.135, current mu = 0.007) allocation failure scavenge might not succeed
transform[stdout]: [8506:0x19f2a90]    68515 ms: Mark-sweep 1390.3 (1437.3) -> 1387.8 (1455.8) MB, 2511.2 / 0.0 ms  (average mu = 0.075, current mu = 0.005) allocation failure scavenge might not succeed
transform[stdout]: 
transform[stdout]: 
transform[stdout]: <--- JS stacktrace --->
transform[stdout]: 
transform[stdout]: ==== JS stack trace =========================================
transform[stdout]: 
transform[stdout]:     0: ExitFrame [pc: 0x21440d25452b]
transform[stdout]: Security context: 0x151eda32ee11 <JSObject>
transform[stdout]:     1: new NodePath [0x91e8a7e4669] [/mnt/c/Users/aless/Desktop/UMNSVP/freya-app/new_branch/node_modules/@babel/traverse/lib/path/index.js:~59] [pc=0x21440d4a75b7](this=0x3afbd5beda71 <NodePath map = 0x15f88580ba49>,hub=0x048270b825d9 <undefined>,parent=0x251db6f78119 <Node map = 0x15f885836291>)
transform[stdout]:     2: ConstructFrame [pc: 0x21440d199dc9]
transform[stdout]:     3: /* anonymous...
transform[stdout]: 
INFO
03:13
transform[stderr]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
INFO
03:13
transform[stderr]:  1: 0x7fa28e94d46c node::Abort() [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  2: 0x7fa28e94d4b5  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  3: 0x7fa28eb79e6a v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  4: 0x7fa28eb7a0e1 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  5: 0x7fa28ef14c66  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  6: 0x7fa28ef26043 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  7: 0x7fa28ef26930 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  8: 0x7fa28ef2891d v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]:  9: 0x7fa28ef28975 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]: 10: 0x7fa28eef4dda v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:13
transform[stderr]: 11: 0x7fa28f180424 v8::internal::Runtime_AllocateInTargetSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/lib/x86_64-linux-gnu/libnode.so.64]
transform[stderr]: 12: 0x21440d25452b 
INFO
03:14
transform[stdout]: 
transform[stdout]: <--- Last few GCs --->
transform[stdout]: 
transform[stdout]: [8516:0x902a90]    68473 ms: Scavenge 1380.1 (1421.3) -> 1379.8 (1421.8) MB, 4.9 / 0.0 ms  (average mu = 0.101, current mu = 0.033) allocation failure 
transform[stdout]: [8516:0x902a90]    68482 ms: Scavenge 1380.5 (1421.8) -> 1380.2 (1422.8) MB, 4.9 / 0.0 ms  (average mu = 0.101, current mu = 0.033) allocation failure 
transform[stdout]: [8516:0x902a90]    68490 ms: Scavenge 1380.8 (1422.8) -> 1380.5 (1423.3) MB, 4.8 / 0.0 ms  (average mu = 0.101, current mu = 0.033) allocation failure 
transform[stdout]: 
transform[stdout]: 
transform[stdout]: <--- JS stacktrace --->
transform[stdout]: 
transform[stdout]: ==== JS stack trace =========================================
transform[stdout]: 
transform[stdout]:     0: ExitFrame [pc: 0x6609d5d452b]
transform[stdout]: Security context: 0x1c8f4932ee11 <JSObject>
transform[stdout]:     1: /* anonymous */ [0xb2a7406f381] [/mnt/c/Users/aless/Desktop/UMNSVP/freya-app/new_branch/node_modules/@babel/traverse/lib/index.js:~75] [pc=0x6609d8687ab](this=0x3d6b1fee0e11 <JSFunction traverse (sfi = 0xbd7d0dec4d1)>,node=0x3f1c1c2c8599 <Node map = 0x3a29e80b98e1>,opts=0x3d6b1fee6479 <Object map = 0x3a29e80ae201>,scope=0x26e2353f0af9 <Scope map = 0x3a...
transform[stdout]: 
INFO
03:14
transform[stderr]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
INFO
03:14
transform[stderr]:  1: 0x7f2ebd76d46c node::Abort() [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  2: 0x7f2ebd76d4b5  [/lib/x86_64-linux-gnu/libnode.so.64]
transform[stderr]:  3: 0x7f2ebd999e6a v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  4: 0x7f2ebd99a0e1 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  5: 0x7f2ebdd34c66  [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  6: 0x7f2ebdd46043 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  7: 0x7f2ebdd46930 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  8: 0x7f2ebdd4891d v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]:  9: 0x7f2ebdd48975 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]: 10: 0x7f2ebdd14dda v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/lib/x86_64-linux-gnu/libnode.so.64]
INFO
03:14
transform[stderr]: 11: 0x7f2ebdfa031e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/lib/x86_64-linux-gnu/libnode.so.64]
transform[stderr]: 12: 0x6609d5d452b

看起来aws-sdk中的某个东西正在进行某种调用,但一直失败,所以它一直在重试调用,直到超过它可以重试调用的次数。我不知道这是什么,也不知道为什么会发生,但任何帮助都将不胜感激!我有我的App.js文件如下。

import React, {Component} from 'react';
import { Button, Dimensions, StatusBar, StyleSheet, Text, TextInput, Platform, View, Image } from 'react-native';
import VerticalViewPager from 'react-native-vertical-view-pager/src/component/VerticalViewPager';
import { NavigationContainer } from '@react-navigation/native';
import Test from './Test.js';
import Car from './Car.js';
import Menu from './Menu.js';
import Charging from './Charging.js';

import Aws from 'aws-sdk/dist/aws-sdk-react-native';
import awsIot from 'aws-iot-device-sdk';
import './shim.js';

const AWS_REGION = 'us-east-2';
const AWS_COGNITO_IDENTITY_POOL = 'us-east-2:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
const AWS_IOT_ENDPOINT = 'XXXXXXXXXXXX.iot.us-east-2.amazonaws.com';

Aws.config.region = AWS_REGION
Aws.config.credentials = new Aws.CognitoIdentityCredentials({
    IdentityPoolId: AWS_COGNITO_IDENTITY_POOL
})

export default class App extends React.Component {
    constructor(props) {
        super(props);
        this.IotConnect();
    }

    IoTConnect() {
        Aws.config.credentials.get(() => {
            const config = {}
            let client
            
            config.host = AWS_IOT_ENDPOINT
            config.protocol = 'wss'
            config.clientId = `client-${Math.floor((Math.random() * 100000) + 1)}`
            config.accessKeyId = 'XXXXXXXXXXXXXX';
            config.secretKey = 'XXXXXXXXXXXXXXXXXXX';
            config.sessionToken = Aws.config.credentials.sessionToken
            
            client = awsIot.device(config)
            
            client.on('connect', () => {
                console.log("Successful connection");
            })
               
            client.on('error', error => {
                console.log(error)
            })
        })
    }

    render() {
      return (
        <NavigationContainer>
            <VerticalViewPager showsVerticalScrollIndicator={false}>
                <Test></Test>
                <Car></Car>
                <Menu></Menu>
                <Charging></Charging>
            </VerticalViewPager>
        </NavigationContainer>
      );
    }
}
mklgxw1f

mklgxw1f1#

经过一段时间的折腾,我终于找到了问题所在。不知什么原因,expo/React不喜欢导入import Aws from 'aws-sdk/dist/aws-sdk-react-native';,所以我将其改为import Aws from 'aws-sdk'。即使使用了更改后的导入,我仍然可以调用Aws.config,所以不需要对代码进行重大更改。我还将以下代码块移到了IoTConnect函数中:

Aws.config.credentials = new Aws.CognitoIdentityCredentials({
    IdentityPoolId: AWS_COGNITO_IDENTITY_POOL
})

我不确定这是否会影响它的运行,但安全总比遗憾好。
我注意到这个问题只在import Aws from 'aws-sdk/dist/aws-sdk-react-native';导入时出现过,这表明它出于某种原因破坏了应用程序,我假设导入被反复调用,直到堆中没有任何空间。

kxkpmulp

kxkpmulp2#

我通过在babel.config.js中为该特定文件添加一个ignore config来解决同样的问题:

{...
 ignore: ["node_modules/aws-sdk/dist/aws-sdk-react-native.js"],
 ...}

相关问题