- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在端口 5000 上有一个 Express 后端服务器,并在端口 3000 上运行 react 前端。我试图从 Express Post 路由中获取一些数据并将其返回到前端,但我的 Promise 从未解析。它总是以“停滞”告终。
util.inspect(messageList) 在服务器控制台上显示我的数组,但我在前端的 Promise 永远不会解析。
我正在 ComponentDidMount 上获取服务器端的一些数据,如下所示:
class Conversation extends React.Component {
state = {
conversations: [],
messages: [],
error: null,
loading: true,
input: '',
owner: 'Unassigned'
}
componentDidMount() {
const { match } = this.props
const { conversationId } = match.params
// Make a POST request to our server and pass the conversationId
this.getMessages(conversationId)
}
getMessages(conversationId) {
return fetch('/search-conversation', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ conversation: conversationId })
})
.then(res => res.json())
.then((messages) => this.setState({ messages }))
}
服务器端:
app.post('/search-conversation', (req, res) => {
conversationId = req.body.conversation
if (!conversationId) {
res.send('/error');
} else {
console.log(`Success, conv id is ${conversationId}`);
}
// call function to go get messages from API
console.log(`fetching messages for ${conversationId}`)
return fetch(endpoint)
.then((res) => res.json())
.then(({ data }) => data)
.then((data) => {
const messageList = data[0].messages.data
return messageList
})
.then((messageList) => console.log(util.inspect(messageList)))
.catch(error => console.error(`Error: ${error}`))
});
任何想法都表示赞赏,提前致谢。
最佳答案
您缺少res.json()在服务器端调用将响应发送到客户端:
app.post('/search-conversation', (req, res) => {
conversationId = req.body.conversation
if (!conversationId) {
res.send('/error');
} else {
console.log(`Success, conv id is ${conversationId}`);
}
// call function to go get messages from API
console.log(`fetching messages for ${conversationId}`)
return fetch(endpoint)
.then((res) => res.json())
.then(({ data }) => data)
.then((data) => {
const messageList = data[0].messages.data
res.json(messageList) // <-- sending response
})
.catch(error => console.error(`Error: ${error}`))
});
关于javascript - Express/React promise 陷入停滞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861306/
我们遇到了 PHPUnit 有时无法在 CI 中正确运行我们的测试套件的问题。该行为表现为大约 1/3 的测试未能启动并且 CI 步骤超时。有趣的是,我们没有看到这种行为: 在我们的机器上本地 每次我
ExecutorService 中的所有线程都忙于等待执行程序服务队列中卡住的任务。 示例代码: ExecutorService es=Executors.newFixedThreadPool(8);
我有一个正在使用的工具,我需要它来运行解析器并输出另一个分析日志。目前我有它,所以它是通过网络界面。 用户转到表单并提交文件名进行解析(文件已在系统中)。 表单向Python CGI脚本提交信息 Py
两天来我一直在努力解决这个问题,但没有得到评估。任何帮助将不胜感激。 我安装了以下内容: eclipse android-sdk-macosx Android SDK 工具 Android 2.2 A
我在我的 package.json 中编写了以下测试命令: "scripts": { "test": "./node_modules/.bin/jasmine-node spec", }, 当
我是一名优秀的程序员,十分优秀!