So I am trying to get some information from a database to populate a view. However whenever I try to call this information I always seem to get an undefined value and thus, the view doesn't populate.
I have looked at How do I return the response from an asynchronous call? and make use of the then function as stated to do for angular already, however it did not help me fix what is wrong.
public getMyObject (ownerId: number): Promise<MyObject[]> {
return this.httpClient.post('ObjectGivenownerID.php', `{ownerID: ${ownerId}}`)
.then(data => {
console.log(data);
return data;
})
The problem is data is always undefined, so when my view calls this to be initialized the array it requires resolves as undefined. I want to ask if the error is in getting the data, or elsewhere? I am fairly new to working with databases and the like so an explanation and a pointer in the right direction would be appreciated more than a "Did it for you" solution. Thanks!
EDIT : PHP Code
function get_owner($owner_id){
global $conn;
$sql = "SELECT * FROM MyObject WHERE owner_id = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// create an array
$emparray = array();
while($row = $result->fetch_assoc()) {
$MyObject_id = $row["MyObject_id"];
$MyObject_attachments = list_attachments_by_MyObject($MyObject_id);
$row['attachments']= $MyObject_attachments;
$emparray[] = $row;
}
}
echo json_encode($emparray);
return;
}
get_owner(1)