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
node.js express knex.js
add a comment |
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
node.js express knex.js
Possible duplicate of How do I return the response from an asynchronous call?
– str
Nov 11 at 11:45
add a comment |
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
node.js express knex.js
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
node.js express knex.js
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
add a comment |
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
add a comment |
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);
})
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
add a comment |
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
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
|
show 1 more comment
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);
})
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
add a comment |
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);
})
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
add a comment |
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);
})
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);
})
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
add a comment |
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
add a comment |
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
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
|
show 1 more comment
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of How do I return the response from an asynchronous call?
– str
Nov 11 at 11:45