javascript - Insert into database using knex -


i using knex 0.13.0 , trying insert mysql database following function:

async function create(title, description) {     //trim spaces     console.log("title: " + title)     console.log("description: " + description)     title = title.trim()     description = description.trim()     createdat = _.now()     deleted = false     console.log("create post: " + title + " " + description + " " + createdat + " " + deleted)      if (title.length < 1 || title.length > 255) throw new error('title not valid.')     if (description.length < 1) throw new error('description not valid.')      try {         await knex('posts').insert({             title,             description,             createdat,             deleted         })         console.log("added db")         return true;     } catch (e) {         return "an error occured: " + e;     } } 

the last console output create post: title description 1505062847788 falseis shown right, nothing happening, after waiting

  • i guess asynch part of function, else in meanwhile?
  • is there standard way create entry when using knex?

appreciate reply!

i'm using node 6, can't test 'await' @ moment (came in node 7) this post looks should assign await response variable. like:

...         var awresponse; // new variable try {     awresponse = await knex('posts').insert({ ... 

in detail:

async function create(title, description) {     //trim spaces     console.log("title: " + title)     console.log("description: " + description)     title = title.trim()     description = description.trim()     createdat = _.now()     deleted = false     console.log("create post: " + title + " " + description + " " + createdat + " " + deleted)      if (title.length < 1 || title.length > 255) throw new error('title not valid.')     if (description.length < 1) throw new error('description not valid.')      var awresponse; // new variable     try {         awresponse = await knex('posts').insert({             title,             description,             createdat,             deleted         })         console.log("added db")         return true;     } catch (e) {         return "an error occured: " + e;     } } 

what have should work fine, i've been doing (as alternative you) directly using promises, , constructing data access functions follows:

function create(title, description) {     return promise.resolve().then(function () {         // first section preping record insert.         //         //trim spaces         console.log("title: " + title)         console.log("description: " + description)         title = title.trim()         description = description.trim()         // createdat = _.now()  // have error "_" not valid         createdat = (new date()).toisostring();         deleted = false         console.log("create post: " + title + " " + description + " " + createdat + " " + deleted)          if (title.length < 1 || title.length > 255) throw new error('title not valid.')         if (description.length < 1) throw new error('description not valid.')         return { "title": title,                  "description": description,                  "createdat": createdat,                 "deleted": deleted };     })     .then(function (recordtoinsert) {         // second section insert.         //         console.log("part #2");         return knex('posts').insert(recordtoinsert)         .on('query-error', function(ex, obj) {             // console.log("knex query-error ex:", ex);             // console.log("knex query-error obj:", obj);             // below logs db errors custom encapsulation of winston logging.             //       ... , .catch further down still executed.             log.logmsg('error', "dba.ins88", "knex create.on.query-error", {"fnc": "create", "obj":obj, "ex":ex} );         })     })     .then(function (insertresult) {         // third section post-processing result (if needed).         //         console.log("part #3 added db :", insertresult);         return insertresult; // returns id value insert;     })     .catch(function (e) {         // omit .catch let caller know , handle exceptions         console.log( "an error occured: " + e);     }); }; 

hope helps!


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -