0

i would like to POST a json to the server side in react native.

my fetch function is like this

fetch(BASE_URL+'contactSync', {
       method: 'POST',
       headers: {
         'Accept': 'application/json',
         'Content-Type':'application/json'
       },
       body:JSON.stringify(unSyncContacts),
      }).then(response => response.json().then(function(responseText) {
          console.log("responseText:",responseText)
          console.log("Length",responseText.data.length)

          if(responseText.message==="Success")
            {
              // this.setState({visible:false})

              for(let i=0;i<responseText.data.length;i++)
                {
                  //let newContact = allContacts.filtered('contId = "'+responseText.data[i].contId+'"');


                       realm.write(() => {
                       realm.create('ContactTable',{
                         contId:responseText.data[i].contId,
                         serverId:responseText.data[i].serverId,
                         isSync:responseText.data[i].isSync,
                       },true);
                       console.log("Record Update","True");
                    });                    
                     if(i == responseText.data.length-1)
                     {
                       let allNewContacts = realm.objects('ContactTable');
                       console.log("Data base",allNewContacts)
                       this.setState({visible:false,messageLable:'Sync successfully Completed.'});
                       Alert.alert(Tag,"Contact successfully synced");
                     }
                }
         }
       })).then((responseData) =>{

     }).catch((error) => {
       this.setState({visible:false})
       if(error.message==="Network request failed")
       {
           setTimeout(()=>{
             Alert.alert(Tag,error.message)
           },1000)
       }
       console.log("Error",JSON.stringify(error));
      }).done();

body:JSON.stringify(unSyncContacts),

this is what i am sending to the server and server returns data according my request. please help me to solve this query. Thank you.

Swapnil Panchal
  • 387
  • 2
  • 5
  • 21

0 Answers0