3

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

Ricardo Raz
  • 493
  • 1
  • 10
  • 23

1 Answers1

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.

Gpak
  • 3,342
  • 2
  • 21
  • 19
Mika Puhakka
  • 51
  • 2
  • 3
  • The question here wasn't exactly what I was doing, but this pointed me in the right direction. It does indeed seem like "____ is not a function" is the error message that pops up when the knex config is missing or not what it should be. – groovecoder Aug 02 '18 at 16:29
  • 1
    What was your issue in the end? I'm getting the same error but only while trying to run `sails lift` in my Jest test suite; the actual app works perfectly fine. – bzupnick Oct 23 '19 at 03:17