React and this.props.results.map is not a function
up vote
2
down vote
favorite
after tried to set a simple search in the logs show this:
everything seems very ok and standardized so a couple of searches i still have any idea why this error is happening.
i've tried with fetch and the same result
please, someone can elucidate, why this error is happening?
the code:
import React, Component from "react";
import axios from "axios";
import Suggestions from "./suggestions";
class Search extends Component
constructor(props)
super(props);
this.state =
term: "",
error: false,
results:
;
onChange(e)
this.setState(
term: e.target.value ,
() =>
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
);
render()
return (
<div id="searchbox">
<div>
<form>
<input
ref=input =>
this.search = input;
value=this.state.term
onChange=this.onChange.bind(this)
type="text"
placeholder="Search..."
/>
<button type="submit">
<i className="search icon" />
</button>
<Suggestions results=this.state.results />
</form>
</div>
</div>
);
export default Search;
the suggestion
import React from "react";
const Suggestions = props =>
const resultList = props.results.map(r => <li key=r.id>r.title</li>);
return <ul>resultList</ul>;
;
export default Suggestions;
response
javascript reactjs axios
add a comment |
up vote
2
down vote
favorite
after tried to set a simple search in the logs show this:
everything seems very ok and standardized so a couple of searches i still have any idea why this error is happening.
i've tried with fetch and the same result
please, someone can elucidate, why this error is happening?
the code:
import React, Component from "react";
import axios from "axios";
import Suggestions from "./suggestions";
class Search extends Component
constructor(props)
super(props);
this.state =
term: "",
error: false,
results:
;
onChange(e)
this.setState(
term: e.target.value ,
() =>
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
);
render()
return (
<div id="searchbox">
<div>
<form>
<input
ref=input =>
this.search = input;
value=this.state.term
onChange=this.onChange.bind(this)
type="text"
placeholder="Search..."
/>
<button type="submit">
<i className="search icon" />
</button>
<Suggestions results=this.state.results />
</form>
</div>
</div>
);
export default Search;
the suggestion
import React from "react";
const Suggestions = props =>
const resultList = props.results.map(r => <li key=r.id>r.title</li>);
return <ul>resultList</ul>;
;
export default Suggestions;
response
javascript reactjs axios
1
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.res.data.result
. Maybe the response is also empty if there are no results for the search.
– Tholle
Nov 9 at 21:59
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
1
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
after tried to set a simple search in the logs show this:
everything seems very ok and standardized so a couple of searches i still have any idea why this error is happening.
i've tried with fetch and the same result
please, someone can elucidate, why this error is happening?
the code:
import React, Component from "react";
import axios from "axios";
import Suggestions from "./suggestions";
class Search extends Component
constructor(props)
super(props);
this.state =
term: "",
error: false,
results:
;
onChange(e)
this.setState(
term: e.target.value ,
() =>
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
);
render()
return (
<div id="searchbox">
<div>
<form>
<input
ref=input =>
this.search = input;
value=this.state.term
onChange=this.onChange.bind(this)
type="text"
placeholder="Search..."
/>
<button type="submit">
<i className="search icon" />
</button>
<Suggestions results=this.state.results />
</form>
</div>
</div>
);
export default Search;
the suggestion
import React from "react";
const Suggestions = props =>
const resultList = props.results.map(r => <li key=r.id>r.title</li>);
return <ul>resultList</ul>;
;
export default Suggestions;
response
javascript reactjs axios
after tried to set a simple search in the logs show this:
everything seems very ok and standardized so a couple of searches i still have any idea why this error is happening.
i've tried with fetch and the same result
please, someone can elucidate, why this error is happening?
the code:
import React, Component from "react";
import axios from "axios";
import Suggestions from "./suggestions";
class Search extends Component
constructor(props)
super(props);
this.state =
term: "",
error: false,
results:
;
onChange(e)
this.setState(
term: e.target.value ,
() =>
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
);
render()
return (
<div id="searchbox">
<div>
<form>
<input
ref=input =>
this.search = input;
value=this.state.term
onChange=this.onChange.bind(this)
type="text"
placeholder="Search..."
/>
<button type="submit">
<i className="search icon" />
</button>
<Suggestions results=this.state.results />
</form>
</div>
</div>
);
export default Search;
the suggestion
import React from "react";
const Suggestions = props =>
const resultList = props.results.map(r => <li key=r.id>r.title</li>);
return <ul>resultList</ul>;
;
export default Suggestions;
response
javascript reactjs axios
javascript reactjs axios
edited Nov 9 at 22:08
asked Nov 9 at 21:57
lula
16510
16510
1
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.res.data.result
. Maybe the response is also empty if there are no results for the search.
– Tholle
Nov 9 at 21:59
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
1
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14
add a comment |
1
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.res.data.result
. Maybe the response is also empty if there are no results for the search.
– Tholle
Nov 9 at 21:59
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
1
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14
1
1
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.
res.data.result
. Maybe the response is also empty if there are no results for the search.– Tholle
Nov 9 at 21:59
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.
res.data.result
. Maybe the response is also empty if there are no results for the search.– Tholle
Nov 9 at 21:59
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
1
1
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
res.data
will give you the entire parsed JSON response, but you want the array that is the value of the posts
property instead.
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
add a comment |
up vote
1
down vote
IMO you need use
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
and not
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
res.data
will give you the entire parsed JSON response, but you want the array that is the value of the posts
property instead.
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
add a comment |
up vote
2
down vote
accepted
res.data
will give you the entire parsed JSON response, but you want the array that is the value of the posts
property instead.
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
res.data
will give you the entire parsed JSON response, but you want the array that is the value of the posts
property instead.
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
res.data
will give you the entire parsed JSON response, but you want the array that is the value of the posts
property instead.
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
answered Nov 9 at 22:14
Tholle
33k53558
33k53558
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
add a comment |
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
1
1
Tholle, thank you man.
– lula
Nov 10 at 6:22
Tholle, thank you man.
– lula
Nov 10 at 6:22
@lula You're welcome!
– Tholle
Nov 10 at 10:46
@lula You're welcome!
– Tholle
Nov 10 at 10:46
add a comment |
up vote
1
down vote
IMO you need use
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
and not
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
add a comment |
up vote
1
down vote
IMO you need use
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
and not
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
add a comment |
up vote
1
down vote
up vote
1
down vote
IMO you need use
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
and not
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
IMO you need use
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data.posts ))
.catch(() => this.setState( error: true ));
and not
axios
.get("/search?q=" + this.state.term)
.then(res => this.setState( results: res.data ))
.catch(() => this.setState( error: true ));
answered Nov 9 at 22:12
A.Vincent
367
367
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233758%2freact-and-this-props-results-map-is-not-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What does your response data look like? Maybe the response is an object, and you need to access the array with e.g.
res.data.result
. Maybe the response is also empty if there are no results for the search.– Tholle
Nov 9 at 21:59
@Tholle is a array of posts, i gonna edit and post the response screen. i changed results to posts and the same
– lula
Nov 9 at 22:07
1
change .then(res => this.setState( results: res.data )) to res.data.posts
– SakoBu
Nov 9 at 22:11
@SakoBu thank you very much. :)
– lula
Nov 9 at 22:14