How can I store a knex query in a variable?

user3674401

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
thebearingedge

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP - How can I store variable states?

From Dev

How can I store the output of summary in a variable?

From Dev

How can I store dataset() in Application variable?

From Dev

How can I store my variable in a file?

From Dev

How can I store the result of a function into a variable?

From Dev

How can store variable while select query in LINQ?

From Dev

How can i use the querystring for knex?

From Dev

How do I store a mongodb query result in a variable?

From Dev

Can I conditionally add a where() clause to my knex query?

From Dev

How to store a query result in a variable

From Dev

How can I query for multiple aggregates from event store?

From Dev

How can I store a 'join' query as a new table in SQLite?

From Dev

How can I store the result of MongoDB query in a text file with Nodejs?

From Dev

How can I store a variable length array in a struct in C

From Dev

How can I store a method in a variable in Java 8?

From Dev

How can I store string from object as a variable?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

How can I store an object in MeteorJS/React in a variable to return later?

From Dev

How can I store the value a user enters in a variable?

From Dev

How can I store the value of a MySqlCommand in C# to a local variable?

From Dev

How can I store a string literal to a variable with no substitution or interpretation?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

how can i store the printing result, obtained by "for statement" variable?

From Dev

How can I store a permanent variable on my website?

From Dev

How can I store output string to a variable AND display in console

From Dev

How i can destroy an GameObject but also store the gameobject in a variable

From Dev

can I declare variable in a query?

From Dev

can I declare variable in a query?

From Dev

Swfit - How can I use the UserDefaults to store variable and how to load the variable from UserDefaults next time

Related Related

  1. 1

    PHP - How can I store variable states?

  2. 2

    How can I store the output of summary in a variable?

  3. 3

    How can I store dataset() in Application variable?

  4. 4

    How can I store my variable in a file?

  5. 5

    How can I store the result of a function into a variable?

  6. 6

    How can store variable while select query in LINQ?

  7. 7

    How can i use the querystring for knex?

  8. 8

    How do I store a mongodb query result in a variable?

  9. 9

    Can I conditionally add a where() clause to my knex query?

  10. 10

    How to store a query result in a variable

  11. 11

    How can I query for multiple aggregates from event store?

  12. 12

    How can I store a 'join' query as a new table in SQLite?

  13. 13

    How can I store the result of MongoDB query in a text file with Nodejs?

  14. 14

    How can I store a variable length array in a struct in C

  15. 15

    How can I store a method in a variable in Java 8?

  16. 16

    How can I store string from object as a variable?

  17. 17

    How can I store a result of a function in a variable in Python?

  18. 18

    How can I store an object in MeteorJS/React in a variable to return later?

  19. 19

    How can I store the value a user enters in a variable?

  20. 20

    How can I store the value of a MySqlCommand in C# to a local variable?

  21. 21

    How can I store a string literal to a variable with no substitution or interpretation?

  22. 22

    How can I store a result of a function in a variable in Python?

  23. 23

    how can i store the printing result, obtained by "for statement" variable?

  24. 24

    How can I store a permanent variable on my website?

  25. 25

    How can I store output string to a variable AND display in console

  26. 26

    How i can destroy an GameObject but also store the gameobject in a variable

  27. 27

    can I declare variable in a query?

  28. 28

    can I declare variable in a query?

  29. 29

    Swfit - How can I use the UserDefaults to store variable and how to load the variable from UserDefaults next time

HotTag

Archive