Updating a Morph-One Associated model and receiving “Unhandled rejection Error”

℡╲_俬逩灬. 提交于 2020-04-30 06:38:06

问题


Code

Preferred way

this.related('title').save({value: input});

But as this line is cut and pasted from a middle of some abstract class, below is a more decoupled way of directly reproducing the same error message.

Alternative Implementation

let title = await book.related('title');
title.set({value: inputs.title});
title.save().then( (model) => {} );

Error message

Unhandled rejection Error: Undefined binding(s) detected when compiling SELECT. Undefined column(s): [titles.titleable_id] query: select distinct `titles`.* from `titles` where `titles`.`id` = ? and `titles`.`titleable_id` = ? and `titles`.`titleable_type` = ? limit ?
    at QueryCompiler_MySQL.toSQL (/Users/captainhusaynpinguin/Documents/sails/ilog/node_modules/knex/lib/query/compiler.js:101:13)
    at Builder.toSQL (/Users/captainhusaynpinguin/Documents/sails/ilog/node_modules/knex/lib/query/builder.js:77:44)
    at /Users/captainhusaynpinguin/Documents/sails/ilog/node_modules/knex/lib/runner.js:31:36
    at /Users/captainhusaynpinguin/Documents/sails/ilog/node_modules/knex/lib/runner.js:260:24

Notice: though the above error is printed in the terminal, because of JavaScript's way of performing nested functions, the program actually updates the row in the database and continues with rendering the response view. However this error can cause breaking the program in the middle (displaying a 500 view) when the same functionality is in combination with await ...

Background

Bookshelf.js is hooked to Sails-JS via a custom hook:

model: Title

let Post = require('../Post')

var Title = sails.hooks.orm.bookshelf.Model.extend({
    tableName: 'titles',
    hasTimestamps: true,
    titleable() {
        return this.morphTo('titleable', 'Post')
    }
});

module.exports = sails.hooks.orm.bookshelf.model('Title', Title);

model: Post

let Post = sails.hooks.orm.bookshelf.Model.extend(
    tableName: 'posts',
    title() {
         return this.morphOne('Title', 'titleable', ['titleable_type', 'titleable_id'])
    }
);

module.exports = Post;
  • MacOS Sierra
  • mysql Ver 8.0.18 for osx10.12 on x86_64 (Homebrew)
  • node v12.13.1
  • Sails v1.2.4
  • bookshelf@1.1.1
  • knex@0.21.0

Other unanswered questions appearing to address similar issue/bug

Error: Undefined binding(s) detected when compiling SELECT from Bookshelf.js save()

Possible explanation

As the api documentation notices, the save() method performs an automatic retrieve of the model, by which time I think Bookshelf fails to pass the initial details (titleable_type and titleable_id) over to knex.

PS. I'm just beginning to use JS for backend, so, apologies in advance if the error is caused by some beginner mistakes/misunderstandings of basic concepts.


回答1:


No idea why the above explained path results in such an error, but for the time being below implementing, does the job without raising any error:

post.load('title').then(function(model) {
    model.related('title').save({value: input});
    return model;
});

Feel free to edit this answer & add more detail/explanation.



来源:https://stackoverflow.com/questions/61312488/updating-a-morph-one-associated-model-and-receiving-unhandled-rejection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!