I have controller and model file. I want to return response of promises from model to controller. In response of my model function conole.log(response) giving me my result but i am not able to send it to back to controller function.
My controller function is
let filters;
for(var i = 0; i < 10; i++){
filters = await ModelFile.getProductOptions(i);
console.log(filters); // undefined
}
My Model Function is
getProductOptions = (product_id) => {
connection.query(query1,function(err,rows) {
var response = [];
//doing something with rows
Promise.all(rows.map(function(item) {
var promise = new Promise(function(resolve,reject) {
connection.query(queryItem,function(err,rows) {
//doing something
result = rows[0].field;
//and want to push it to an array
resolve(result);
});
});
return promise.then(function(result) {
console.log(result); //ok
response.push(result) //ok
});
}).then(function () {
console.log(response); //ok console is showing the response but when return response it gives undefined in controller function
});
});
}