0

I have a simple demo of a React app using to show a simple list of Recipes

I had this all working but it suddenly stopped and I'm getting this the console

TypeError: Cannot read property 'map' of undefined

Everything works in the playground so I'm thinking it must be something in the React side

index.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App';

import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import { ApolloClient } from 'apollo-client'
import { ApolloProvider } from 'react-apollo'
import { ApolloProvider as ApolloProviderHooks } from 'react-apollo-hooks'

const cache = new InMemoryCache()
const link = new HttpLink({
  uri: 'http://locahost:4000'
})

const client = new ApolloClient({
  cache,
  link
})

const Root = () => (
  <Router>
    <Switch>
      <Route path='/' component={App} />
      <Redirect to='/' />
    </Switch>
  </Router>
)


ReactDOM.render(
  <ApolloProvider client={client}>
    <ApolloProviderHooks client={client}>
      <Root />
    </ApolloProviderHooks>
  </ApolloProvider>
  , document.getElementById('root'));

queries/index.tsx

export const GET_ALL_RECIPES = gql`
  query RecipeData{
    recipe{
      _id
      name
      description
    }
  }
`

generated/RecipeData.tsx

export interface RecipeData_recipe {
  __typename: "Recipe";
  _id: string | null;
  name: string | null;
  description: string | null;
}

export interface RecipeData {
  recipe: (RecipeData_recipe | null)[] | null;

App.tsx

import React from 'react';
import './App.css';

import { RecipeData } from '../generated/RecipeData';
import { GET_ALL_RECIPES } from '../queries';
import { useQuery } from 'react-apollo-hooks';


const App: React.FC = () => {

  const { data, loading } = useQuery<RecipeData | null>(GET_ALL_RECIPES, {
    suspend: false
  })

  if (loading || !data) return <div>Loading</div>

  return (
    <div className="App">
      <ul>
        {
          data.recipe !== null && data.recipe.map(recipe => (
            <li>{recipe.name}</li>
          ))
        }
      </ul>

    </div>
  );
}

export default App;

The error from the useQuery is

Error: Network error: Failed to fetch
    at new ApolloError (ApolloError.ts:46)
    at ObservableQuery.getCurrentResult (ObservableQuery.ts:199)
    at useQuery.js:65
    at updateMemo (react-dom.development.js:16854)
    at Object.useMemo (react-dom.development.js:17334)
    at useMemo (react.development.js:1643)
    at useQuery (useQuery.js:57)
    at App (App.tsx:8)
    at renderWithHooks (react-dom.development.js:16260)
    at updateFunctionComponent (react-dom.development.js:18347)
    at beginWork$1 (react-dom.development.js:20176)
    at beginWork$$1 (react-dom.development.js:25756)
    at performUnitOfWork (react-dom.development.js:24695)
    at workLoopSync (react-dom.development.js:24671)
    at performSyncWorkOnRoot (react-dom.development.js:24270)
    at react-dom.development.js:12199
    at unstable_runWithPriority (scheduler.development.js:697)
    at runWithPriority$2 (react-dom.development.js:12149)
    at flushSyncCallbackQueueImpl (react-dom.development.js:12194)
    at flushSyncCallbackQueue (react-dom.development.js:12182)
    at scheduleUpdateOnFiber (react-dom.development.js:23709)
    at dispatchAction (react-dom.development.js:17076)
    at useQuery.js:109
    at actHack (actHack.js:2)
    at Object.invalidateCurrentResult (useQuery.js:108)
    at notifySubscription (Observable.js:140)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.error (Observable.js:240)
    at ObservableQuery.ts:701
    at Array.forEach (<anonymous>)
    at iterateObserversSafely (ObservableQuery.ts:701)
    at Object.onError [as error] (ObservableQuery.ts:627)
    at invoke (QueryManager.ts:518)
    at QueryManager.ts:576
    at QueryManager.ts:1091
    at Set.forEach (<anonymous>)
    at QueryManager.ts:1087
    at Map.forEach (<anonymous>)
    at QueryManager.broadcastQueries (QueryManager.ts:1085)
    at QueryManager.ts:434

Can anyone see anything that I'm doing wrong

2 Answers 2

1

Try changing:

data.recipe !== null && data.recipe.map(recipe => (

to:

data.recipe && data.recipe.map(recipe => (

this will include both null and undefined check.

Sign up to request clarification or add additional context in comments.

1 Comment

This stops the console error but it doesn't display the list
1

data will be undefined when a network error is encountered during the request and a response cannot be obtained. Check the error object returned by the hook, or take a look at the raw response from the server inside your browser's dev tools to determine what the actual error is.

3 Comments

In the dev tools the Network tabs says localhost failed but doesn't give any other info in the tabs. The console returns Failed to load resource: net::ERR_NAME_NOT_RESOLVED locahost:4000/:1
I have updated the question with the error object returned by the hook
@cdmt locahost is not a valid DNS name as the error indicates. Presumably you meant localhost

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.