javascript - Knex resolving empty set after insert -
i'm trying setup basic database connection in node using postgresql , knex, i'm having trouble getting seems simple insert work. i'm basing of code on example code in knex github repo.
the issue seems first insert (where add admin user) resolving empty set of rows (not sure since documentation doesn't contain info on rows or row sets far can find).
here code:
const knex = require("knex")({ client: "postgres", connection: { host : "127.0.0.1", user : "postgres", password : "my password goes here", database : "issue-tracker", charset : "utf8" } }); knex.schema .droptableifexists("tickets") .droptableifexists("users") .createtable("users", createusertable) .createtable("tickets", createtickettable) .then(() => addtestdata()) .then(() => console.log("done")) .catch((e) => console.log(e)); function createusertable(table) { table.increments("id"); table.string("username").index().unique(); table.string("password"); table.timestamps(); } function createtickettable(table) { table.increments("id"); table.timestamps(); table.integer("creator_id").unsigned().references("users.id"); table.string("text"); } function addtestdata() { return promise.resolve() .then(() => knex.insert({username: "admin", password: "password"}) .into("users") ) .then((rows) => // rows empty? knex.insert({creator_id: rows[0], text: "this test"}) .into("tickets") ) .then(() => knex("users").join("tickets", "users.id", "tickets.creator_id") .select("users.id creator_id", "users.username creator_name", "tickets.text text") ) .then(console.log.bind(console)); }
any assistance appreciated.
promise handlers must return something. works assembly line - each station must put results of work onto line.
- insert operations in knex not return rows unless explicitly tell them to. knex has
.returning()
(docs). - your last handler in chain (
console.log.bind(console)
) returns nothing, hence final result of chain undefined. - you not need start promise chain
promise.resolve()
. can start promise-returning function - usingknex()
directly sensible here.
so, re-arranged, end this:
function addtestdata() { return knex("users") .returning("user_id") .insert({username: "admin", password: "password"}) .then((rows) => { return knex("tickets") .returning("ticket_id") .insert({creator_id: rows[0], text: "this test"}); }) .then((rows) => { return knex("users") .join("tickets", "users.id", "tickets.creator_id") .select("users.id creator_id", "users.username creator_name", "tickets.text text"); }) .then((rows) => { console.log.bind(console); return rows; }); }
this equivalent this, return
implied , therefore less obvious:
function addtestdata() { return knex("users") .returning("user_id") .insert({username: "admin", password: "password"}) .then((rows) => knex("tickets") .returning("ticket_id") .insert({creator_id: rows[0], text: "this test"}); }) .then((rows) => knex("users") .join("tickets", "users.id", "tickets.creator_id") .select("users.id creator_id", "users.username creator_name", "tickets.text text"); }) .then((rows) => { console.log.bind(console); return rows; }); }
Comments
Post a Comment