Knejx Js only promises











up vote
0
down vote

favorite












how to make this function work. Only a promise comes back to me.



codeProducts.forEach((code, index) => {

const qt = app.db('products').where('code',code).first().then(result => result.quantity)
data[index] = {
code: code,
quantity: qt
}
})


return data









share|improve this question
























  • Possible duplicate of How do I return the response from an asynchronous call?
    – str
    Nov 11 at 11:45















up vote
0
down vote

favorite












how to make this function work. Only a promise comes back to me.



codeProducts.forEach((code, index) => {

const qt = app.db('products').where('code',code).first().then(result => result.quantity)
data[index] = {
code: code,
quantity: qt
}
})


return data









share|improve this question
























  • Possible duplicate of How do I return the response from an asynchronous call?
    – str
    Nov 11 at 11:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











how to make this function work. Only a promise comes back to me.



codeProducts.forEach((code, index) => {

const qt = app.db('products').where('code',code).first().then(result => result.quantity)
data[index] = {
code: code,
quantity: qt
}
})


return data









share|improve this question















how to make this function work. Only a promise comes back to me.



codeProducts.forEach((code, index) => {

const qt = app.db('products').where('code',code).first().then(result => result.quantity)
data[index] = {
code: code,
quantity: qt
}
})


return data






node.js express knex.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:42









Fazal Rasel

2,9631725




2,9631725










asked Nov 11 at 11:31









Guilherme De Menezes Ferreira

51




51












  • Possible duplicate of How do I return the response from an asynchronous call?
    – str
    Nov 11 at 11:45


















  • Possible duplicate of How do I return the response from an asynchronous call?
    – str
    Nov 11 at 11:45
















Possible duplicate of How do I return the response from an asynchronous call?
– str
Nov 11 at 11:45




Possible duplicate of How do I return the response from an asynchronous call?
– str
Nov 11 at 11:45












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










There is two or three ways that you can solve this-



Approach: 1



I will make just one call to database.(I love my database :D) as-



let codeProducts = [55, 68, 96];
knex('products')
.whereIn('code', codeProducts)
.then((results) => {
// response to api call
console.log(results);
});


Approach: 2 (I don't like this approach. Too many call on db)



async function getData(codes) {
try {
let results = ;
for (let i = 0; i < codes.length; i++) {
let dbQuery = await knex('products').where('code', codes[i]).first();
results.push(dbQuery);
}
return results;
} catch (e) {
console.log(e);
}
}


const codeProducts = [54, 95];
getData()
.then((res) => {
console.log(res);
})





share|improve this answer























  • finally worked, thank you !!!! saved my life
    – Guilherme De Menezes Ferreira
    Nov 12 at 10:45










  • Welcome to Stackoverflow.
    – Fazal Rasel
    Nov 12 at 10:47


















up vote
0
down vote













let data = codeProducts.map((code, index) => {

return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})


return data


This code should fix your problem. You are accessing the quantity outside of the promise. In order to set the quantity on the data array, you need to do it inside the then






share|improve this answer























  • not, return array empty.
    – Guilherme De Menezes Ferreira
    Nov 11 at 11:48










  • Checkout my new version, I think now it's ok, i wanted map not foreach
    – Pedro Silva
    Nov 11 at 12:17










  • not, return [ undefined, undefined ] :(
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:22










  • I'm not being able to find an answer.
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:23










  • I needed the return for the promise too in order to propagate the result, my bad
    – Pedro Silva
    Nov 11 at 12:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248295%2fknejx-js-only-promises%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










There is two or three ways that you can solve this-



Approach: 1



I will make just one call to database.(I love my database :D) as-



let codeProducts = [55, 68, 96];
knex('products')
.whereIn('code', codeProducts)
.then((results) => {
// response to api call
console.log(results);
});


Approach: 2 (I don't like this approach. Too many call on db)



async function getData(codes) {
try {
let results = ;
for (let i = 0; i < codes.length; i++) {
let dbQuery = await knex('products').where('code', codes[i]).first();
results.push(dbQuery);
}
return results;
} catch (e) {
console.log(e);
}
}


const codeProducts = [54, 95];
getData()
.then((res) => {
console.log(res);
})





share|improve this answer























  • finally worked, thank you !!!! saved my life
    – Guilherme De Menezes Ferreira
    Nov 12 at 10:45










  • Welcome to Stackoverflow.
    – Fazal Rasel
    Nov 12 at 10:47















up vote
0
down vote



accepted










There is two or three ways that you can solve this-



Approach: 1



I will make just one call to database.(I love my database :D) as-



let codeProducts = [55, 68, 96];
knex('products')
.whereIn('code', codeProducts)
.then((results) => {
// response to api call
console.log(results);
});


Approach: 2 (I don't like this approach. Too many call on db)



async function getData(codes) {
try {
let results = ;
for (let i = 0; i < codes.length; i++) {
let dbQuery = await knex('products').where('code', codes[i]).first();
results.push(dbQuery);
}
return results;
} catch (e) {
console.log(e);
}
}


const codeProducts = [54, 95];
getData()
.then((res) => {
console.log(res);
})





share|improve this answer























  • finally worked, thank you !!!! saved my life
    – Guilherme De Menezes Ferreira
    Nov 12 at 10:45










  • Welcome to Stackoverflow.
    – Fazal Rasel
    Nov 12 at 10:47













up vote
0
down vote



accepted







up vote
0
down vote



accepted






There is two or three ways that you can solve this-



Approach: 1



I will make just one call to database.(I love my database :D) as-



let codeProducts = [55, 68, 96];
knex('products')
.whereIn('code', codeProducts)
.then((results) => {
// response to api call
console.log(results);
});


Approach: 2 (I don't like this approach. Too many call on db)



async function getData(codes) {
try {
let results = ;
for (let i = 0; i < codes.length; i++) {
let dbQuery = await knex('products').where('code', codes[i]).first();
results.push(dbQuery);
}
return results;
} catch (e) {
console.log(e);
}
}


const codeProducts = [54, 95];
getData()
.then((res) => {
console.log(res);
})





share|improve this answer














There is two or three ways that you can solve this-



Approach: 1



I will make just one call to database.(I love my database :D) as-



let codeProducts = [55, 68, 96];
knex('products')
.whereIn('code', codeProducts)
.then((results) => {
// response to api call
console.log(results);
});


Approach: 2 (I don't like this approach. Too many call on db)



async function getData(codes) {
try {
let results = ;
for (let i = 0; i < codes.length; i++) {
let dbQuery = await knex('products').where('code', codes[i]).first();
results.push(dbQuery);
}
return results;
} catch (e) {
console.log(e);
}
}


const codeProducts = [54, 95];
getData()
.then((res) => {
console.log(res);
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 9:41

























answered Nov 12 at 6:59









Fazal Rasel

2,9631725




2,9631725












  • finally worked, thank you !!!! saved my life
    – Guilherme De Menezes Ferreira
    Nov 12 at 10:45










  • Welcome to Stackoverflow.
    – Fazal Rasel
    Nov 12 at 10:47


















  • finally worked, thank you !!!! saved my life
    – Guilherme De Menezes Ferreira
    Nov 12 at 10:45










  • Welcome to Stackoverflow.
    – Fazal Rasel
    Nov 12 at 10:47
















finally worked, thank you !!!! saved my life
– Guilherme De Menezes Ferreira
Nov 12 at 10:45




finally worked, thank you !!!! saved my life
– Guilherme De Menezes Ferreira
Nov 12 at 10:45












Welcome to Stackoverflow.
– Fazal Rasel
Nov 12 at 10:47




Welcome to Stackoverflow.
– Fazal Rasel
Nov 12 at 10:47












up vote
0
down vote













let data = codeProducts.map((code, index) => {

return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})


return data


This code should fix your problem. You are accessing the quantity outside of the promise. In order to set the quantity on the data array, you need to do it inside the then






share|improve this answer























  • not, return array empty.
    – Guilherme De Menezes Ferreira
    Nov 11 at 11:48










  • Checkout my new version, I think now it's ok, i wanted map not foreach
    – Pedro Silva
    Nov 11 at 12:17










  • not, return [ undefined, undefined ] :(
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:22










  • I'm not being able to find an answer.
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:23










  • I needed the return for the promise too in order to propagate the result, my bad
    – Pedro Silva
    Nov 11 at 12:33















up vote
0
down vote













let data = codeProducts.map((code, index) => {

return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})


return data


This code should fix your problem. You are accessing the quantity outside of the promise. In order to set the quantity on the data array, you need to do it inside the then






share|improve this answer























  • not, return array empty.
    – Guilherme De Menezes Ferreira
    Nov 11 at 11:48










  • Checkout my new version, I think now it's ok, i wanted map not foreach
    – Pedro Silva
    Nov 11 at 12:17










  • not, return [ undefined, undefined ] :(
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:22










  • I'm not being able to find an answer.
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:23










  • I needed the return for the promise too in order to propagate the result, my bad
    – Pedro Silva
    Nov 11 at 12:33













up vote
0
down vote










up vote
0
down vote









let data = codeProducts.map((code, index) => {

return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})


return data


This code should fix your problem. You are accessing the quantity outside of the promise. In order to set the quantity on the data array, you need to do it inside the then






share|improve this answer














let data = codeProducts.map((code, index) => {

return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})


return data


This code should fix your problem. You are accessing the quantity outside of the promise. In order to set the quantity on the data array, you need to do it inside the then







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 12:32

























answered Nov 11 at 11:42









Pedro Silva

38811




38811












  • not, return array empty.
    – Guilherme De Menezes Ferreira
    Nov 11 at 11:48










  • Checkout my new version, I think now it's ok, i wanted map not foreach
    – Pedro Silva
    Nov 11 at 12:17










  • not, return [ undefined, undefined ] :(
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:22










  • I'm not being able to find an answer.
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:23










  • I needed the return for the promise too in order to propagate the result, my bad
    – Pedro Silva
    Nov 11 at 12:33


















  • not, return array empty.
    – Guilherme De Menezes Ferreira
    Nov 11 at 11:48










  • Checkout my new version, I think now it's ok, i wanted map not foreach
    – Pedro Silva
    Nov 11 at 12:17










  • not, return [ undefined, undefined ] :(
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:22










  • I'm not being able to find an answer.
    – Guilherme De Menezes Ferreira
    Nov 11 at 12:23










  • I needed the return for the promise too in order to propagate the result, my bad
    – Pedro Silva
    Nov 11 at 12:33
















not, return array empty.
– Guilherme De Menezes Ferreira
Nov 11 at 11:48




not, return array empty.
– Guilherme De Menezes Ferreira
Nov 11 at 11:48












Checkout my new version, I think now it's ok, i wanted map not foreach
– Pedro Silva
Nov 11 at 12:17




Checkout my new version, I think now it's ok, i wanted map not foreach
– Pedro Silva
Nov 11 at 12:17












not, return [ undefined, undefined ] :(
– Guilherme De Menezes Ferreira
Nov 11 at 12:22




not, return [ undefined, undefined ] :(
– Guilherme De Menezes Ferreira
Nov 11 at 12:22












I'm not being able to find an answer.
– Guilherme De Menezes Ferreira
Nov 11 at 12:23




I'm not being able to find an answer.
– Guilherme De Menezes Ferreira
Nov 11 at 12:23












I needed the return for the promise too in order to propagate the result, my bad
– Pedro Silva
Nov 11 at 12:33




I needed the return for the promise too in order to propagate the result, my bad
– Pedro Silva
Nov 11 at 12:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248295%2fknejx-js-only-promises%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python