【发布时间】:2017-09-01 16:55:56
【问题描述】:
我正在尝试使用 axios 从获取请求中打印 SQL 数据。我正在使用 express 服务器端,我的前端是 React。在我的反应组件中,我有一个具有 axios GET 调用的函数,然后我在渲染中调用该函数。我可以得到很好的数据。我的问题实际上是将数据打印到表中。到目前为止,这是我的代码:
getTableData(){
axios.get("/api")
.then(function (response){
return(
Object.keys(response).map( (row, index) => (
<TableRow key={index} selected="false">
<TableRowColumn>Test</TableRowColumn>
<TableRowColumn>Test</TableRowColumn>
</TableRow>
))
)
})
.catch(function (error){
console.log(error);
})
}
这是我用来执行 API 调用以及尝试打印表格的函数。我在渲染函数中将其称为 {this.getTabledata()}。
这是我 server.js 中的 get 请求:
app.get('/api', function (req, res){
sql.connect(config, err =>{
new sql.Request().query('select * from Counselling', (err, result) =>{
var table = new Object();
result["recordset"].map( (row, index) => (
table[row["StudentName"]] = row["StudentNumber"]
));
res.send(table);
sql.close();
});
});
我有什么遗漏吗?我必须对行使用特定的映射函数吗?
【问题讨论】:
-
如果您在每次重新渲染时都从渲染中调用它,则您需要调用数据库。如果您的组件经常更改,成本可能会很高。
标签: javascript reactjs express material-ui axios