使用redux和rails API处理post请求



我正试图在API中发布一些带有fetch方法的数据。

export const CREATE_MATCH = 'CREATE_MATCH'
export function createMatch(user) {
const request = fetch("/api/matches", {
// Adding method type
method: "POST",

// Adding headers to the request
headers: {
"Content-type": "application/json",
"X-User-Token": user.authentication_token,
"X-User-Email": user.email
}
})
return {
type: CREATE_MATCH,
payload: request
}
}

但我只得到了响应,而没有得到创建的数据

响应{type:"basic",url:";http://localhost:3000/api/matches",重定向:false,状态:200,确定:true,…}

我不知道如何获得创建的数据。

在rails中,这就是我所拥有的,我在Match中没有任何数据,只有id和时间戳。

def create
@match = Match.new
authorize @match
if @match.save
render json: @match
else
render_error
end
end

我刚刚用异步/等待函数找到了答案

export async function createMatch(user) {
const request = await fetch("/api/matches", {
// Adding method type
method: "POST",
// Adding body or contents to send
// body: JSON.stringify(),
// Adding headers to the request
headers: {
"Content-type": "application/json",
"X-User-Token": user.authentication_token,
"X-User-Email": user.email
}
})
const match = await request.json();
console.log(match)
return {
type: CREATE_MATCH,
payload: match
}
}

最新更新