为什么我的抓取响应不会在我的调度中通过?
Why won't my fetch response by passed in my dispatch?
我正在尝试使用节点服务器作为 firebase 和我的 React 本机应用程序之间的中介。
有人可以告诉我我的提取有什么问题吗?
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => {
console.log(response.json());
//dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: response.json()});
});
};
};
这是 node/express 路由器:
const router = (app) => {
app.get('/', (req, res) => {
firebase.database().ref('/social/posts')
.once('value', snapshot => res.json(snapshot.val()));
});
};
如果我 console.log
response.json()
那么我就得到这个:
如果我 console.log 回应,我得到这个:
如何摆脱 headers?如果我这样做 console.log(response._bodyInit)
然后我得到这个:
这看起来像我需要的。
但是如果我将它作为有效负载传递,则会收到此错误:
我以前的 action creator 直接使用 firebase,像这样:
export const fetchPostsByNewest = () => {
return (dispatch) => {
firebase.database().ref('/social/posts')
.once('value', snapshot => {
console.log(snapshot.val())
dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};
如果我console.log(snapshot.val())
。然后我得到了这个:
这很好用,看起来与 console.log(response._bodyInit)
的最后一个 console.log 相同。
我在这里做错了什么?我真的很感激任何帮助。
谢谢!
Fetch 的工作方式略有不同,当您调用 response.json()
时 return 是另一个承诺,因此您需要在 return 新承诺之后链接另一个 then
:
fetch('http://localhost:3090/')
.then(response => response.json())
.then(aFunctionToDoSomethingWithTheJson);
response.json()
returns 一个承诺,所以你需要另一个 link 在承诺链中
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => response.json())
.then((json) => {
console.log(json);
dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: json});
});
};
};
如果您不需要 console.log json
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => response.json())
.then((json) => dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: json}));
};
};
我正在尝试使用节点服务器作为 firebase 和我的 React 本机应用程序之间的中介。
有人可以告诉我我的提取有什么问题吗?
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => {
console.log(response.json());
//dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: response.json()});
});
};
};
这是 node/express 路由器:
const router = (app) => {
app.get('/', (req, res) => {
firebase.database().ref('/social/posts')
.once('value', snapshot => res.json(snapshot.val()));
});
};
如果我 console.log
response.json()
那么我就得到这个:
如果我 console.log 回应,我得到这个:
如何摆脱 headers?如果我这样做 console.log(response._bodyInit)
然后我得到这个:
这看起来像我需要的。
但是如果我将它作为有效负载传递,则会收到此错误:
我以前的 action creator 直接使用 firebase,像这样:
export const fetchPostsByNewest = () => {
return (dispatch) => {
firebase.database().ref('/social/posts')
.once('value', snapshot => {
console.log(snapshot.val())
dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};
如果我console.log(snapshot.val())
。然后我得到了这个:
这很好用,看起来与 console.log(response._bodyInit)
的最后一个 console.log 相同。
我在这里做错了什么?我真的很感激任何帮助。
谢谢!
Fetch 的工作方式略有不同,当您调用 response.json()
时 return 是另一个承诺,因此您需要在 return 新承诺之后链接另一个 then
:
fetch('http://localhost:3090/')
.then(response => response.json())
.then(aFunctionToDoSomethingWithTheJson);
response.json()
returns 一个承诺,所以你需要另一个 link 在承诺链中
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => response.json())
.then((json) => {
console.log(json);
dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: json});
});
};
};
如果您不需要 console.log json
export const fetchPostsByNewest = () => {
return (dispatch) => {
fetch('http://localhost:3090/')
.then((response) => response.json())
.then((json) => dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: json}));
};
};