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")