Javascript 使用reactjs从restapi访问数据
我正在用react开发一个应用程序,我发现在处理RESTAPI中的信息时遇到了一个大问题 我有一个fetch,其中api by get方法应该为我带来客户id,但我总是会遇到这样的错误 {错误:“未找到客户”} 错误 : “找不到客户” 我想得到一个只有所有客户id的数组,为此我做了这个fetch函数,但我不知道要放入什么。然后((resposneValue)来得到正确的数组。我尝试了map函数,但是我得到了一个错误,比如map prototiping没有定义 这是我的密码:Javascript 使用reactjs从restapi访问数据,javascript,api,reactjs,fetch,Javascript,Api,Reactjs,Fetch,我正在用react开发一个应用程序,我发现在处理RESTAPI中的信息时遇到了一个大问题 我有一个fetch,其中api by get方法应该为我带来客户id,但我总是会遇到这样的错误 {错误:“未找到客户”} 错误 : “找不到客户” 我想得到一个只有所有客户id的数组,为此我做了这个fetch函数,但我不知道要放入什么。然后((resposneValue)来得到正确的数组。我尝试了map函数,但是我得到了一个错误,比如map prototiping没有定义 这是我的密码: getHisto
getHistory() {
const {customer} = this.state
fetch(
DOMAIN+'/api/customers/'+customer._id, {
method: 'get',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer '+this.props.token
}
})
.then((response) =>
{
return response.json();
})
.then((responseValue) => {
/*responseValue.map(function (v) {
v.platform = v.app ? v.app.platform : null })*/
this.setState({customsId:responseValue});
console.log(responseValue);
})
}
编辑:
我还有一个收获:
getCustomers(){
fetch(
DOMAIN+'/api/customers/shop/'+this.props.salon, {
method: 'get',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer '+this.props.token
}
})
.then((response) =>
{
console.log(this.props.salon);
return response.json();
})
.then((responseData) => {
responseData.map(function (v) {
v.platform = v.app ? v.app.platform : null })
this.setState({customers:responseData});
//console.log(responseData);
})
.catch(function() {
console.log("error");
});
}
然后函数中的console.log向我报告此数组:
(22) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{_id: "591d66f69ea5d935ed4b9ba9", shop: {…}, email:...
因此,问题是,我如何通过第一次提取或第二次提取访问该id值?这是API错误。这意味着您发送的请求中包含无效的客户。\u id,请检查此参数。以下是我的解决方法: 我创建了这个抓取
getCustomers(){
fetch(
DOMAIN+'/api/customers/shop/'+this.props.salon, {
method: 'get',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer '+this.props.token
}
})
.then((response) =>
{
return response.json();
})
.then((responseData) => {
responseData.map(function (v) {
v.platform = v.app ? v.app.platform : null })
this.setState({customers:responseData});
//console.log(responseData);
})
.catch(function() {
console.log("error");
});
}
然后,我创建了一个handleClick来启动函数,并使用该行。:\u id我可以打印id
handleCellClick(y,x,row){
//this.getHistory(this.state.orders);
this.getHistory();
var ab= this.state.orders
this.setState({
open:true,
slideIndex: 0,
newForm:false,
customer:{...row,_id:row._id},
order:{...x,customer:x.customer}
});
this.getCustomers(row._id);
console.log(row._id);
console.log(x.customer);
}
看起来您的api没有返回您传递的客户id的客户详细信息。好的,当我运行另一个类似这样的获取:DOMAIN+'/api/customers/shop/'+this.props.salon时,我接收到客户id,如{u id:“591d66f69ea5d935ed4b9ba9”,shop:{…},