0

This was working before but not sure why im getting this error, i checked the url, my server end with the function call, i even changed the return data but still get this error. I explain below.

I have this fetch method in getData():

const getData = () => {

    setOnRefresh(true);
    fetch('https://soppinga.com/api/Home?guid=' + route.params.id + "&userId=" + "1", {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }})
        .then((resp) => {resp.json(); console.log(resp); console.log(resp.json())    })
        .then((json) => {loadData(json); setOnRefresh(false);})
        .catch((error) => {  console.log(">>> " + error); })
        .finally(() => null);
    
}

The component initializes using this:

useEffect(() => {
    
    getData();
    navigation.setOptions({
        headerLargeTitle: true,
        headerRight: () => (
            
            <TouchableOpacity style={{marginRight:15,}} onPress={() => getData()}>
            <MaterialCommunityIcons name="refresh" color={"#ffffff"} size={26} />
            </TouchableOpacity>
            
        ),
    });
}, [navigation]);

I tested the call in the browser and i do get the data consistently returning:

{
  "poll": [
    {
      "userId": "1",
      "id": 1,
      "guid": "xxx-xxx-xxx-xxx-xxx",
      "question": "A Question 1",
      "description": "A Description 1",
      "date": "AAAAAAAAF3s=",
      "videoId": "eU4ZvfkmOck",
      "options": "{\"options\":[\"Yes\",\"No\"]}",
      "value": null,
      "voteCount": 0
    }
  ],
  "votes": [{ "voteCount": 1, "option": "Yes", "percentage": 100 }]
}

This is my server end function that gets called in the fetch function:

[HttpGet]
public IActionResult Get(string guid, string userId)
{

    var ctx = new SmurfdbContext();

    object pollItem = (from poll in ctx.TblPolls
                            // Filter on polls.Guid before the join, for performance and simplicity.
                        // Filter on vote.UserId and vote.PollGuid before the join, for performance and simplicity.
                        join vote in ctx.TblVotes.Where(v => v.UserId == userId && v.PollGuid == guid) on poll.Guid equals vote.PollGuid into gg
                        where poll.Guid == guid
                        from vote in gg.DefaultIfEmpty() // At this point vote will be null if gg was empty.
                        select new
                        {
                            poll.UserId,
                            poll.Id, // Not shown in question.
                            poll.Guid,
                            poll.Question,
                            poll.Description, // Not shown in question.
                            poll.Date, // Not shown in question.
                            poll.VideoId, // Not shown in question.
                                            //What is vote.Value?  It isn't shown in the question so I don't know how to fix this line:
                                            //value = gg.Where(x => x.UserId == userId && x.PollGuid == guid).FirstOrDefault().Value,
                                            //But possibly this is correct:
                            poll.Options,
                            value = vote.Value,
                            VoteCount = gg.Count(),
                        }
                ).ToList();


    object votesMadeItem = ctx.TblVotes.Where(x => x.PollGuid == guid).GroupBy(x => x.Value)
        .Select(x => 
        new { voteCount = x.Count(y => y.Value == x.Key), option = x.Key, 
            percentage = Math.Round((Math.Round(Decimal.Parse(x.Count(y => y.Value == x.Key).ToString()), 2) / ctx.TblVotes.Where(x => x.PollGuid == guid).Count()) * 100, 2),  }).ToList();


    return Ok(new { poll = pollItem, votes = votesMadeItem });
}

And i keep receiving this error:

LOG  {"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "899cf805-36fc-4090-90f8-cad8a8d16566", "offset": 0, "size": 263}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "899cf805-36fc-4090-90f8-cad8a8d16566", "offset": 0, "size": 263}}, "bodyUsed": true, "headers": {"map": {"content-type": "application/json; 
charset=utf-8", "date": "Sat, 08 Jul 2023 00:14:29 GMT", "server": "Microsoft-IIS/10.0", "x-powered-by": "ASP.NET"}}, "ok": true, "status": 200, "statusText": "", "type": "default", "url": "https://soppinga.com/api/Home?guid=a2efb21a-6c33-4c60-9ed6-da5592e93119&userId=1"}
LOG  {"_h": 0, "_i": 0, "_j": null, "_k": null}
LOG  undefined
ERROR  There was a problem sending log messages to your development environment [PrettyFormatPluginError: undefined is not a function]
LOG  >>> TypeError: Cannot read property 'votes' of undefined
WARN  Possible Unhandled Promise Rejection (id: 3):
TypeError: Already read
TypeError: Already read
    at consumed (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:4840:44)
    at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:4949:32)
    at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:4969:25)
    at anonymous (http://localhost:8081/Screens\VoteItem.bundle//&platform=android&app=com.smurfrn&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:101:30)
    at tryCallOne (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/2q6r403w/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:53:16)
    at anonymous (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/2q6r403w/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:139:27)
    at apply (native)
    at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:29637:26)
    at _callTimer (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:29556:17)
    at _callReactNativeMicrotasksPass (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:29586:17)
    at callReactNativeMicrotasks (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:29749:44)
    at __callReactNativeMicrotasks (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:2779:46)
    at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:2591:45)
    at __guard (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:2763:15)
    at flushedQueue (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:2590:21)
    at callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.smurfrn&modulesOnly=false&runModule=true:2575:33)

The poll and votes have data, also if they dont have data then an empty array will be returned.

return Ok(new { poll = pollItem, votes = votesMadeItem });

If i remove the votes part in the server end function i get this error about the poll:

LOG  >>> TypeError: Cannot read property 'poll' of undefined

I dont understand because the call was working, i have not changed anything, but i receive this error any body know what is going on?

if anymore information in code is needed please ask.

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
redoc01
  • 2,107
  • 5
  • 32
  • 64

1 Answers1

1

Looking in stackoverflow i found this:

https://stackoverflow.com/questions/38842499/react-native-possible-unhandled-promise-rejection

And changed my fetch to this instead:

return fetch('https://soppinga.com/api/Home?guid=' + route.params.id + "&userId=" + "1", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json'
        }})
    .then(function(response){
      return response.json();
    })
    .then(function(json){
        loadData(json); setOnRefresh(false);
    })
    .catch(function(error) {
    console.log('There has been a problem with your fetch operation: ' + error.message);
     // ADD THIS THROW error
      throw error;
    });

Now works.

redoc01
  • 2,107
  • 5
  • 32
  • 64
  • Not sure why in this component this occurs as im using the fetch as explained in the question in all other components – redoc01 Jun 20 '23 at 19:39