I'm trying to get some data from a mysql database.
The backend seems to work fine, the data is being logged there but not in the frontend.
app.post('/api/user/database_test', (req, res, next) => {
var result;
database.query('SELECT * FROM test', function (error, response, fields) {
if (error) throw error;
console.log('response:', response);
result = response;
console.log('result: ', result) //works fine
})
res.status(200).json({
message: 'Data from Database :',
result: result
})
});
This is displayed in the backend console:
result: [
RowDataPacket {
userid: '1',
username: 'testuser',
firstname: 'testname',
lasname: 'testname2',
email: '[email protected]',
}
]
But i also need to display the data i the frontend so i tried it like this.
async getFromDB() {
var obj = 'test';
this.http.post('http://localhost:3000/api/user/database_test', obj)
.subscribe((response) => {
console.log('response: ', response)
})
}
But i only receive the message 'Data from database' in the browser console but no actual data. I also tried to log response.result but the code does not compile anymore.
What am I doing wrong?
asyncoff your method, returning the Observable in your service method, and subscribing to it in the component.