我试图从使用axios的获取请求打印SQL数据。我使用快速服务器端,我的前端是React。在我的反应组件中,我有一个具有axios GET调用的函数,然后在渲染中调用该函数。我可以把数据弄好。我的问题实际上是将数据打印到表格中。这是我到目前为止的代码:试图通过Axios从获取请求打印数据
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();
});
});
有我丢失的东西?我是否必须为行使用特定的映射函数?
如果您每次进行重新渲染时都从渲染中调用它,则会进行数据库调用。如果您的组件经常更改,则代价可能很高。 – Dez