【发布时间】:2020-12-03 10:59:38
【问题描述】:
我有一个 handleSubmit 函数,我添加了两个 post 请求。
但是,其中一个发布请求依赖于从第一个发布请求返回的数据。我试图通过将其设置为 var 来访问此数据,但在第二次提取中似乎无法访问。不确定我的语法是否错误..有什么想法吗?
我相信这应该可行,但我不确定我哪里出错了。谢谢!
handleSubmit = () => {
fetch('http://localhost:3000/resources', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
name: this.state.name,
description: this.state.description,
link: this.state.link,
topic_id: this.state.topic_id
})
})
.then(res => res.json())
.then(data => {
this.props.addResource(data)
var dataId = data.id;
})
return fetch('http://localhost:3000/user_resources', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
resource_id: dataId,
user_id: this.props.users.id
})
})
.then(res => res.json())
.then(data => {
this.props.addUserResource(data)
})
this.props.navigation.goBack()
}
【问题讨论】:
标签: react-native promise react-redux fetch