Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 986
Ricardo Raz Avatar asked Jun 28 '26 00:06

Ricardo Raz


1 Answers

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.

like image 175
Mika Puhakka Avatar answered Jun 30 '26 12:06

Mika Puhakka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!