使用Fetch()发布多个用户输入-React Native

时间:2019-03-19 15:23:36

标签: react-native post input hashmap fetch

我正在从API提取文档和数据。每个文档都有多个输入字段。

我正在解析并在视图上显示这些表单,用户将在字段中输入其数据。

如何保存每个状态,并将其发布到我的服务器。

感谢您的任何建议!

这是我的代码:

     renderTextandInputs = (obje) => {

    var keyvalue_to_json = JSON.parse(obje.keyValues);
    var textinputName = [];
    var foundTextFields = [];
    for (let i = 0; i < keyvalue_to_json.inputFields.length; i++) {
      if (keyvalue_to_json.inputFields[i].type === 'textfield') {
        foundTextFields.push(<TextInput onChangeText={(text) => this.postToBmp({ text })} style={{ borderWidth: 1 }}>{keyvalue_to_json.inputFields[i].placeholderText}</TextInput>) &&
          textinputName.push(<Text>{keyvalue_to_json.inputFields[i].title}</Text>)
      }
    }

    return (
      <View>
        <ListItem
          title={obje.name}
          subtitle={obje.description}
          onPress={() => this.postToBmp(obje)}
        />
        <View >
          <View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
            <View style={{ flex: 1}}>
              {textinputName}
            </View>
            <View style={{ flex: 1}}>
              {foundTextFields}
            </View>
      </View>
    )
  }

这是fetch()函数,我正在尝试使用HashMap()进行此工作,但不确定那是否是正确的解决方案:

   postToBmp = (obje) => {
    var postRequest = new HashMap();
    for (myTextField in myList.components) {
      let key = myTextField.key;
      let value = myTextField.value;
      if (key != null && value != null) {
        postRequest.set(key, value);
      }
    }

    fetch('URL', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'Connection': 'Keep-Alive',
      },

      credentials: 'include',
      body: JSON.stringify({

        from: 'test@test.dk',
        to: [
          'test test <test2@test2.com>'
        ],

        attachmentName: obje.name,

        dateString: '19. marts 2019',

        signedByFullName: '',  //I need to pass the values of the inputs

        signedByCPRNumber: '', //I need to pass the values of the inputs

        otherCompanyName: '', //I need to pass the values of the inputs

        otherCompanyCVRNumber: '', //I need to pass the values of the inputs

      })
    })
  }

0 个答案:

没有答案