Unhandled promise error: [object Promise]TypeError: this.db(...) is not a function

Unhandled promise error: [object Promise]TypeError: this.db(...) is not a function

teditorteditor Posts: 6Questions: 2Answers: 0

Hi all,

I've just installed the trial version of DataTables Editor with Node.JS and am trying to run the examples.
When I run:
$ npm start
and open the URL the web page opens normally but there are no entries in the table just "Loading...".
In the console there is this error:

$ npm start

> datatables.net-editor-demo@1.7.4 start /home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4
> node index.js

DataTables Editor demo - navigate to http://localhost:8081/
Unhandled promise error:  [object Promise]TypeError: this.db(...) is not a function
stack: TypeError: this.db(...) is not a function
    at Editor.<anonymous> (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.ts:928:24)
    at step (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:42:23)
    at Object.next (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:23:53)
    at fulfilled (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:14:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Unhandled promise error:  [object Promise]TypeError: this.db(...) is not a function
stack: TypeError: this.db(...) is not a function
    at Editor.<anonymous> (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.ts:928:24)
    at step (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:42:23)
    at Object.next (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:23:53)
    at fulfilled (/home/user/DataTables-Editor-Trial/Editor-NodeJS-1.7.4/node_modules/datatables.net-editor-server/dist/editor.js:14:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)

It seems to be a problem with the database connection.
These are the steps I've followed:
1. $ npm install --save
2. Created with DB Browser for SQLite a new DB and pasted the SQL code from here:
https://editor.datatables.net/examples/sql/sqlite.sql.
The created tables seem to be OK.
3. Modified db.js:

var knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: "./myeditor.db"
  },
  useNullAsDefault: true
});

Any idea what I have done wrong?

This question has an accepted answers - jump to answer

Answers

  • teditorteditor Posts: 6Questions: 2Answers: 0

    I see now there are no records in the tables except in the "todo" table. I tried also with Valentina Studio and I get an error when I run the SQL:

    14:34:03 Kernel error: Sqlite db "/home/user/DataTables-Editor-Trial/editor_db4.db": Error : "12 values for 10 columns".

  • teditorteditor Posts: 6Questions: 2Answers: 0

    Same error with PostgreSQL. I've loaded the postgres.sql file and tables and records seem to be OK.

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin
    Answer ✓

    Are you exporting the knex variable at all?

    The default db.js that I have is:

    let knex = require('knex');
    
    module.exports = knex({
        client: 'mysql',
    
        connection: {
            database: '',
            host: '',
            password: '',
            user: '',
            dateStrings: true
        }
    });
    

    So for your use case it should be:

    let knex = require('knex');
    
    module.exports = knex({
      client: 'sqlite3',
      connection: {
        filename: "./myeditor.db"
      },
      useNullAsDefault: true
    });
    

    Then in the controller it should do something like:

    let db = require('../db');
    ...
    
      let editor = new Editor(db, 'table').fields(
    

    Regards,
    Allan

  • teditorteditor Posts: 6Questions: 2Answers: 0

    @allan : It works now for PostgreSQL! Many thanks!

    I've changed db.js from this:

    var knex = require('knex')({
      client: 'pg',
      version: '10.5',
      connection: {
        host : '127.0.0.1',
        user : 'myuser',
        password : 'mypwd',
        database : 'datatables'
      }
    });
    

    (which I found in the Knex.js docs:
    https://knexjs.org/#Installation-client
    )
    to this:

    let knex = require('knex');
    
    module.exports = knex({
      client: 'pg',
      version: '10.5',
      connection: {
        host : '127.0.0.1',
        user : 'myuser',
        password : 'mypwd',
        database : 'datatables',
        dateStrings: true
    }
    });
    

    I assume the same way would have worked for SQLite but there seems to be an issue with the provided SQL since both DB Browser for SQLite and Valentina Studio failed to create correctly all tables and records.

This discussion has been closed.