I need to get knex
select query result to a variable.
function getUserPlanDetailsWithOutCb(user_id) {
var dataArr =[];
knex('user_plans').select('*').where({ 'user_id': user_id }).then(function(result) {
result.forEach(function(value) {
dataArr.push(value)
});
//return dataArr;
});
return dataArr;
}
var result = getUserPlanDetailsWithOutCb(12);
I have tried return value outside and inside of the call back in knex
. For above code i got the result as [ ]
For second one (return inside callback)
i got the result as
{
"isFulfilled": false,
"isRejected": false
}
I need to get knex
select query result to a variable.
function getUserPlanDetailsWithOutCb(user_id) {
var dataArr =[];
knex('user_plans').select('*').where({ 'user_id': user_id }).then(function(result) {
result.forEach(function(value) {
dataArr.push(value)
});
//return dataArr;
});
return dataArr;
}
var result = getUserPlanDetailsWithOutCb(12);
I have tried return value outside and inside of the call back in knex
. For above code i got the result as [ ]
For second one (return inside callback)
i got the result as
{
"isFulfilled": false,
"isRejected": false
}
Share
Improve this question
edited Feb 1, 2018 at 9:15
reshma
asked Feb 1, 2018 at 8:20
reshmareshma
7221 gold badge10 silver badges26 bronze badges
6
- What is the question? – Waleed Iqbal Commented Feb 1, 2018 at 8:24
- i need to get the result of this query to a variable. Like, var result = getUserPlanDetailsWithOutCb(19); – reshma Commented Feb 1, 2018 at 9:12
- @Pillai It's an asynchronous function, so you have to pass a callback. Or you can use async/await with promises. – Abhyudit Jain Commented Feb 1, 2018 at 9:20
- Have a look at this ... stackoverflow./questions/34094806/return-from-a-promise-then – Waleed Iqbal Commented Feb 1, 2018 at 9:37
- @WaleedIqbal what i need is to assign result to a variable. I need to use that variable again. – reshma Commented Feb 1, 2018 at 10:19
1 Answer
Reset to default 5To get your variable returned using Promises, do the following to the data retrieval function:
And note: The return knex(
returns the Promise object to the caller, and the return dataArr
returns the value to the caller's .then()
promise clause.
function getUserPlanDetailsWithOutCb(user_id) {
var dataArr =[];
return knex('user_plans').select('*')
.where({ 'user_id': user_id })
.then(function(result) {
result.forEach(function(value) {
dataArr.push(value)
});
return dataArr;
});
}
Call the function via:
var result;
var aPromise = getUserPlanDetailsWithOutCb(12)
.then(function(result) {
result = value;
});
The result
variable will be set when the aPromise
resolves.
Calling the function using var aPromise
will cause program execution to continue before the Promise is fulfilled and the result
is set, which may not be what you want. So you can use await
instead (as @Abhyudit Jain notes), but I haven't done that, so I would mess up the syntax for you if I tried.
Cheers! Gary.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744196422a4562678.html
评论列表(0条)