Reputation: 225
render: function(){
console.log(this.state.search_results);
var renderedSearchResults = this.state.search_results.map((result) => {
The console.log prints:
[{"id": "testGroup2"}, {"id": "testGroup77777"}, {"id": "testGroup1"}, {"id": "testGroup3"}]
The data is obtained through:
$.ajax({
url: this.props.routes.search,
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(searchObj),
success: function(data){
console.log(data);
this.setState({search_results:data});
}.bind(this),
error: function(xhr, status,err){
console.error("/api/response", status, err.toString());
}.bind(this)
});```
Through:
def post(self):
"""
Makes a request to search for a specific
searchtype: Group
searchstring: ""
requestedfields: []
"""
search_type = self.json_data.get('searchtype', 'Group')
search_string = self.json_data.get('searchstring', '')
requestedfields = self.json_data.get('requestedfields', ['id'])
search_model = {
'Group': Group(),
'User': User()
}[search_type]
search_fields = {
'Group': ['id', 'tags'],
'User': ['username']
}[search_type]
# check if requestedfields contains valid fields
for field in requestedfields:
if field == 'id':
continue
value = search_model.default().get(field, None)
if value is None:
return self.set_status(400, "Model {0} does not have requested field {1}".format(search_type, field))
try:
search_results = search_model.search_items(search_string, search_fields, requestedfields)
except err:
return self.set_status(400, "Something went wrong")
self.set_status(200, "Success")
return self.write(tornado.escape.json_encode(search_results))
I'm really confused as to how this.state.search_results
isn't an array that I can iterate through, can anyone see what's going on?
I've tried using console.log(Object.prototype.toString.call(data));
inside the success function and I get:
[object String]
Upvotes: 1
Views: 118
Reputation: 12882
Try to set json
data type in an explicit way, while doing your ajax request:
$.ajax({
dataType: 'json',
//...
});
Most probably Intelligent Guess
that is used to detect data type by jQuery, is giving wrong result.
Upvotes: 1
Reputation: 225
Found my answer, didn't set dataType: "json" in my ajax request.
Upvotes: 0