Objection.js - 查询构建器不是使用插入方法的函数 [英] Objection.js - Query builder not a function using insert method

查看:81
本文介绍了Objection.js - 查询构建器不是使用插入方法的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 nodejs 开发的新手,我目前正在我的 postgresql 上练习 CRUD 操作.我使用 Objection.js 进行 ORM 和模型制作.我遵循文档中的一些代码并编辑必要的行,但实际上并没有成功,而是返回此错误:

I'm new to nodejs Development and I currently practicing CRUD operations on my postgresql. I used Objection.js for the ORM and Model making. I follow some codes from the docs and edit necessary lines but I don't actually get it to success instead it returns this error:

builder.knex(...).queryBuilder 不是函数

builder.knex(...).queryBuilder is not a function

我遵循MVC模式,所以我根据它分离文件.

I am following MVC pattern so I seperate the files according to it.

我的控制器:

'use strict';
const Todo = require('./../models/Todo');

class TodoController {
  createTodo() {
    Todo
      .query()
      .insert({
        'title': 'asdasdasda',
        'description': 'sdasdasdasdasdsad',
        'date': '2017-12-12',
        'isActive': true,
      })
      .then(name => {
        console.log(name.description);
      })
      .catch(err => {
        console.log(err);
      });
  }
}

module.exports = TodoController;

Knex 架构:

 knex.schema.createTableIfNotExists('todo', (table) => {
      table.increments();
      table.string('title', 255).notNullable();
      table.text('description').notNullable();
      table.boolean('isActive').defaultTo('false');
      table.datetime('date').notNullable();
      table.timestamp('createdAt').defaultTo(knex.fn.now());
    })

型号:

'use strict';

const { Model } = require('objection');

class Todo extends Model {
  static get tableName() {
    return 'Todo';
  }
}

module.exports = Todo;

server.js:

    ...
    const KnexConfig = require('./knexfile');
    const { Model } = require('objection');
    ...
    ...
    Model.knex(KnexConfig.development);

希望有人能指导我,我还是 nodejs 的新手

Hopefully someone could guide me, I'm still newbie on nodejs

推荐答案

看起来您正在尝试将 knex 配置对象传递给 Model.knex() 而您需要传递实际膝关节实例.

It looks like you're trying to pass the knex configuration object to Model.knex() whereas you need to pass the actual knex instance.

在 server.js 上:

On server.js:

const { Model } = require('objection');
const knex = require('knex');

const KnexConfig = require('./knexfile');

Model.knex(knex(KnexConfig.development);

每当传递给 Objection.js 的 knex 实例不应该是时,就会出现此错误消息.

This error message seems to arise whenever the knex instance passed to Objection.js is not what is should be.

这篇关于Objection.js - 查询构建器不是使用插入方法的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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