更新Morph-One关联模型并接收“未处理的拒绝错误". [英] Updating a Morph-One Associated model and receiving "Unhandled rejection Error"

查看:165
本文介绍了更新Morph-One关联模型并接收“未处理的拒绝错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

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

错误消息

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

注意:尽管以上错误已在终端中显示,但由于JavaScript执行嵌套函数的方式,该程序实际上更新了数据库中的行并继续呈现响应视图.但是,当相同的功能与await ...

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 ...

Bookshelf.js通过自定义钩子连接到Sails-JS:

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

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);

模型:发布

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
  • x86_64(自制)上osx10.12的mysql Ver 8.0.18
  • 节点v12.13.1
  • 风帆v1.2.4
  • bookshelf@1.1.1
  • knex@0.21.0
    • 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
    • 错误:未定义的绑定从Bookshelf.js save()编译SELECT时检测到

      正如api文档所注意到的那样,save()方法执行模型的自动检索,到那时我认为 Bookshelf 无法传递初始详细信息(titleable_typetitleable_id)转到 knex .

      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.我刚刚开始将JS用于后端,因此,如果错误是由一些初学者的错误/对基本概念的误解引起的,请提前道歉.

      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.

      推荐答案

      不知道上面解释的路径为什么会导致这样的错误,但是在下面的实现中,该工作没有引起任何错误:

      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.

      这篇关于更新Morph-One关联模型并接收“未处理的拒绝错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆