2017-09-10 82 views
0

我使用knex 0.13.0,我想用下面的函數插入到一個MySQL數據庫:插入到數據庫中使用knex

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 is not valid.') 
    if (description.length < 1) throw new Error('Description is not valid.') 

    try { 
     await knex('posts').insert({ 
      title, 
      description, 
      createdAt, 
      deleted 
     }) 
     console.log("added to db") 
     return true; 
    } catch (e) { 
     return "An error occured: " + e; 
    } 
} 

Create Post: Title Description 1505062847788 false最後控制檯輸出顯示正確的,但什麼也沒發生,甚至等待後

  • 我想這是函數的異步部分,但在此期間還有什麼其他的做?
  • 使用knex時是否有標準的方法來創建條目?

感謝您的回覆!

回答

1

我使用的是節點6,因此目前無法測試'await'(來自節點7),但是從this post看起來應該將await響應分配給一個變量。像:

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

詳細:

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 is not valid.') 
    if (description.length < 1) throw new Error('Description is not valid.') 

    var awResponse; // new variable 
    try { 
     awResponse = await knex('posts').insert({ 
      title, 
      description, 
      createdAt, 
      deleted 
     }) 
     console.log("added to db") 
     return true; 
    } catch (e) { 
     return "An error occured: " + e; 
    } 
} 

你有什麼應該工作得很好,但我一直在做的事情(爲你的選擇)只是直接使用的承諾,建設我的數據訪問功能一般如下:

function create(title, description) { 
    return Promise.resolve().then(function() { 
     // This first section is for preping the record for insert. 
     // 
     //trim spaces 
     console.log("title: " + title) 
     console.log("description: " + description) 
     title = title.trim() 
     description = description.trim() 
     // createdAt = _.now() // I have a error that "_" is 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 is not valid.') 
     if (description.length < 1) throw new Error('Description is not valid.') 
     return { "title": title, 
       "description": description, 
       "createdAt": createdAt, 
       "deleted": deleted }; 
    }) 
    .then(function (recordToInsert) { 
     // This second section is for the 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 into my custom encapsulation of winston logging. 
      //  ... and the .catch further down will still be executed. 
      log.logMsg('error', "DBA.INS88", "KNEX create.on.query-error", {"fnc": "create", "obj":obj, "ex":ex}); 
     }) 
    }) 
    .then(function (insertResult) { 
     // This third section is for post-processing the result (if needed). 
     // 
     console.log("Part #3 added to db :", insertResult); 
     return insertResult; // returns id value from insert; 
    }) 
    .catch(function (e) { 
     // I omit this .catch to let the caller know about and handle the exceptions 
     console.log("An error occured: " + e); 
    }); 
}; 

希望這有助於!

相關問題