1

I'm facing a huge problem here, and I don't know how to solve it:

I have a simple route (which works) in node.js made with express:

public static create(router: Router, basePath: string) {
    console.log("[SearchRoute::create] Creating routes for /search.");
    // call the function for retrieving the address book results
    router.get(basePath + "/search/:searchString",
        function (req, res, next) {
            console.log(searchUser(req.params.searchString));
        }
    );
}

And I have the searchUser function with a simple query:

function searchUser(searchString) {
    console.log("searchUser Function executed.");
    return myMYSQL.connection.query('SELECT XXX FROM XXX WHERE ?',[searchString],
        function (error, results) {
            if (error) throw error;
            return (results);
        }
    );
}

The problem that I'm facing is:

In searchUser, the result of the query it's giving me 2 objects.

But in the route (the first function), it's only printing me one result in the values index of the result.

I think the error it's in the return of the searchUser function, I'm somehow returning it wrong and missing something.

Thank you so much to all for your help!

Edit:

Can be that i'm not explaining it in a good way, the second function, prints me 2 objects, but the first one only gets one... i don't even know how to explain it, but here we go:

function searchUser(searchString) {
    console.log("searchUser Function executed.");
    return myMYSQL.connection.query('SELECT XXX FROM XXX WHERE ?',[searchString],
        function (error, results) {
            if (error) throw error;

            // Here is returning me 2 objects, i show the response below
            console.log (results);
            return (results);
        }
    );
}

console.log:

0|call-ser | 2017-03-14 12:29:30 +01:00: [ RowDataPacket {
0|call-ser |     pbe_firstname: 'Axel',
0|call-ser |     pbe_lastname: 'Braun'},
0|call-ser |   RowDataPacket {
0|call-ser |     pbe_firstname: 'Axel2',
0|call-ser |     pbe_lastname: 'Braun2'} ]

And when i call the route:

    public static create(router: Router, basePath: string) {
    console.log("[SearchRoute::create] Creating routes for /search.");
    // call the function for retrieving the address book results
    router.get(basePath + "/search/:searchString",
    function (req, res, next) {
    console.log(searchUser(req.params.searchString));
    }
    );
    }

console.log: in the .values index:

values: [ 'Braun' ]

(not the second "braun")

Daniel
  • 330
  • 1
  • 3
  • 11
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Mar 14 '17 at 11:39
  • Not really, the only problem that i'm facing is the array conversion of my function to the route, i've read the answer and it's not the same case – Daniel Mar 14 '17 at 11:41
  • 1
    It's the same case, you just don't see it. You cannot "return" data from an asynchronous call, but that's exactly what you try in your second function. – Tomalak Mar 14 '17 at 11:42
  • Oh okay, i think I'm too "noob" to solve this by myself.... i checked the answer for an hour and i can't manage it to adapt it to my case... can anybody help me please? – Daniel Mar 14 '17 at 12:38
  • I edited my answer... I don't know if I clarify anything with it, but I really don't get the issue... thank you all – Daniel Mar 14 '17 at 12:50

1 Answers1

1

So yeah, you guys were right. I was not handling correctly the concept of asynchronous requests, my whole concept was wrong. Now i rebuild the project with observables and it's working.

Thank you all!

Daniel
  • 330
  • 1
  • 3
  • 11