Javascript 反应本机api调用响应
我正在发出一个http调用请求,由于某种原因,响应有点奇怪 我的电话看起来像:Javascript 反应本机api调用响应,javascript,json,react-native,Javascript,Json,React Native,我正在发出一个http调用请求,由于某种原因,响应有点奇怪 我的电话看起来像: getOne: id => { return axios.get('posts/1', { params: { id: id } }).then(function (response) { return respons
getOne: id => {
return axios.get('posts/1', {
params: {
id: id
}
}).then(function (response) {
return response;
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
return error;
} else if (error.request) {
// The request was made but no response was received
return error.request;
} else {
// Something happened in setting up the request that triggered an Error
return error.message;
}
});
},
从组件上我称之为
categoryApi.getOne(1).then(response => {
if (response.data) {
this.setState({isLoading: false});
console.log(response.data);
this.setState({categories: this.state.categories.push(response.data)});
console.log(this.state.categories);
} else {
this.setState({isLoading: false});
Alert.alert(
'Something wrong happened!',
'My Alert Msg',
[],
{cancelable: true}
)
}
});
在控制台中,响应如下所示:
Object {
"body": "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto",
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"userId": 1,
}
问题是我希望响应是一个对象,如:
{
"body": "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto",
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"userId": 1,
}
我需要更改什么,以使我没有对象{},只有{}请尝试在JS控制台中键入以下内容:
var myObject={a:1,b:2}
然后检查您的新对象:
myObject
它应该显示为:
对象{a:1,b:2}
{}
和对象{}
都是一回事。后者是控制台选择显示JavaScript对象的方式,因为如果单击展开对象,您可以看到与JavaScript对象关联的其他数据:
{…}
a:1
b:2
__原型:对象{…}
查看本教程了解我没有得到你的答案,那么为什么我在使用push将对象添加到数组时不能使用.map。this.state.categories.map((项,键)=>(不工作)js对象上没有本机映射。你可以这样做
object.entries(myObject).map([key,value])=>console.log('key是,'key);console.log('the value is',value);)
我有一个对象数组,实际上,我如何循环遍历每个对象?您提供的代码似乎不适合测量,所以我只将第一个对象添加到数组中,idk为什么事件例如,数据属性中有三个不同的对象: