最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Objection.js - Query builder not a function using insert method - Stack Overflow

programmeradmin1浏览0评论

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 is not a function

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

My controller:

'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 Schema:

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

Model:

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

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

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 is not a function

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

My controller:

'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 Schema:

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

Model:

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

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

Share Improve this question asked Dec 10, 2017 at 15:37 Ricardo RazRicardo Raz 5233 gold badges12 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

On server.js:

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

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

Model.knex(knex(KnexConfig.development));

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

发布评论

评论列表(0)

  1. 暂无评论