本文介绍了放大React Native-使用amplify add api出现重复错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此Amplify指南 https://aws-amplify.github.io/docs/js/tutorials/building-react-native-apps/#connect-to-your-backend-1 和当我使用"aplify add api"创建API时,应用失败.我正在使用"expo",并且正在使用IphoneX进行测试.

I'm using this Amplify guide https://aws-amplify.github.io/docs/js/tutorials/building-react-native-apps/#connect-to-your-backend-1 and when I create an API using "aplify add api" the app fails.I'm using "expo" and I'm using IphoneX for test phase.

我的应用代码是

import React, { Component } from 'react';
import { StyleSheet, Text, Button, View, Alert } from 'react-native';
import Amplify, { API } from 'aws-amplify';
import amplify from './aws-exports';
import awsmobile from './aws-exports';
import { withAuthenticator } from 'aws-amplify-react-native';

Amplify.configure(amplify);
Amplify.configure(awsmobile);

state = { apiResponse: null };

class App extends Component {


  async getSample() {
    const path = "/items"; // you can specify the path
     const apiResponse = await API.get("theListApi" , path); //replace the API name
     console.log('response:' + apiResponse);
     this.setState({ apiResponse });
   }


  render() { 
    return (
      <View style={styles.container}>
      <Text>test</Text>

      <Button title="Send Request" onPress={this.getSample.bind(this)} />
    <Text>Response: {this.state.apiResponse && JSON.stringify(this.state.apiResponse)}</Text>

    </View>
    );
  }
}

export default withAuthenticator(App);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#63C8F1',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

在命令行中执行"expo start"会返回此消息错误:

executing "expo start" the command line return this message error:

jest-haste-map: Haste module naming collision: theListFunction
  The following files share their name; please adjust your hasteImpl:
    * <rootDir>/amplify/backend/function/theListFunction/src/package.json
    * <rootDir>/amplify/#current-cloud-backend/function/theListFunction/src/package.json


Failed to construct transformer:  DuplicateError: Duplicated files or mocks. Please check the console for more info
    at setModule (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:620:17)
    at workerReply (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:691:9)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 391) {
  mockPath1: 'amplify/backend/function/theListFunction/src/package.json',
  mockPath2: 'amplify/#current-cloud-backend/function/theListFunction/src/package.json'
}
(node:1506) UnhandledPromiseRejectionWarning: Error: Duplicated files or mocks. Please check the console for more info
    at setModule (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:620:17)
    at workerReply (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:691:9)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 391)
(node:1506) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1506) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Error: Duplicated files or mocks. Please check the console for more info
    at setModule (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:620:17)
    at workerReply (/Users/j_hen/Documents/jdev/smartApp/sourcecode/mySmaertProject/node_modules/jest-haste-map/build/index.js:691:9)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 391)

怎么了?如何正确使用API​​?

What is wrong? How can I use the API correctly?

推荐答案

Amplify在amplify/#current-cloud-backend/中创建当前云后端配置的副本.

Amplify creates a copy of your current cloud backend configuration in amplify/#current-cloud-backend/.

您不需要这些文件来构建应用程序,因此可以忽略它们以消除错误.

You don't need those files to build your app, so you can ignore them in order to get rid of the error.

为此,您可以创建一个黑名单并将该文件夹添加到其中.在项目的根目录中创建一个rn-cli.config.js文件.

To do so, you can create a blacklist and add the folder to it.Create a rn-cli.config.js file in the root of your project.

./rn-cli.config.js:

const blacklist = require('metro').createBlacklist;

module.exports = {
  resolver: {
    blacklistRE: blacklist([/#current-cloud-backend\/.*/]),
  },
};

参考问题.

(如穆什的回答所述)

module.exports = {
  resolver: {
    blacklistRE: /#current-cloud-backend\/.*/
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
};

如上所述此处.

这篇关于放大React Native-使用amplify add api出现重复错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:56