0

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)
Community
  • 1
  • 1
Erudaki
  • 250
  • 3
  • 16
  • Why don't you have an error handler? This will give you more information on what went wrong. – Michelangelo Aug 30 '16 at 16:19
  • Can you add the php code. It looks like post to ObjectGivenownerID.php returns nothing. – Dave Aug 30 '16 at 16:28
  • I do have an error handler, its just not in the snippit i provided. The error handler is on the next line as a .catch, however it doesn't seem to error out anywhere, or actually throw any errors. – Erudaki Aug 30 '16 at 16:29
  • Shouldn't this `this.httpClient.post` be a `get`? – DeborahK Aug 30 '16 at 16:39
  • @DeborahK http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get Post is better for my overall goal/security – Erudaki Aug 30 '16 at 17:36
  • 1
    Is this Angular 2? If so, then the http.post returns an Observable, not a promise. Or is your httpClient code converting it to a promise? See this post: http://4dev.tech/2016/07/using-http-client-to-integrate-angular2-to-php/ (It uses a `get`, but once you get the `get` working, you can change it back to a `post`.) – DeborahK Aug 30 '16 at 18:02
  • it is angular 2. Forgot to mention that the httpClient has code that does convert it to a promise. I also have a then function that does a .map after the snippit have originally posted (also did not see it at the time of posting as I'm working with someone else's code who is no longer here). Thank you for the link, I shall make a test environment and see if i cant figureout whats wrong with what i have. I currently have it implemented similarly to this https://angular.io/docs/ts/latest/guide/server-communication.html#!#fetch-data but i will try the method you have provided. – Erudaki Aug 30 '16 at 18:23

0 Answers0