is there a way I can store a variable in a knex query? Right now I can console.log a query result by doing:
knex.select().table('users').then(function(result) {
result.forEach(function(value) {
console.log(value);
});
});
But I want to store the query result in a variable - something like this:
var query = knex.select().table('users')
so I can pass it onto a template, manipulate it, etc...
One thing I have tried is creating an empty array and pushing each value into the array, but the array returns empty for some reason:
var dataArr = [];
knex.select().table('users').then(function(result) {
result.forEach(function(value) {
dataArr.push(value)
});
});
console.log(dataArr.length) // returns 0
As @ShanShan mentioned, your knex
query is run asynchronously, so your call to console.log
is being executed before the query results are returned.
You will need to process/use the results after they are passed to then
:
return knex('users') // select * from "users"
.then(function (users) {
return processUsers(users)
})
.then(function (processed) {
// do something with processed users
})
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments