I tried to use Knex.js on Node.js by following code:
var Promise = require("bluebird");
var knex = require("knex")({
client: 'pg',
user : 'username',
database : 'database',
password: "password",
migrations: {
tableName: 'knex_migrations'
},
pool: {
min: 0,
max: 7
}
});
knex.select("*").from("users").then(function(rows){
console.log(rows);
});
But, it threw an error about Runner object as follows:
/path/to/myapp/node_modules/knex/lib/interface.js:27
return new Runner(this).run().then(onFulfilled, onRejected);
^
TypeError: undefined is not a function
at QueryBuilder_PG.Target.then (/path/to/myapp/node_modules/knex/lib/interface.js:27:12)
at Object.<anonymous> (/path/to/myapp/test.js:14:32)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
When I try to use stream, pipe, exec and transaction, same error occurred.
Please tell me how to solve this problem...
I was really careless about configuration of client.
I have mistaken in description of "connection". It have to be written as follows:
var Promise = require("bluebird");
var knex = require("knex")({
client: 'pg',
connection: {
host : '127.0.0.1',
user: 'username',
database: 'database',
password: 'password'
},
migrations:{
tableName:"knex_migrations"
},
pool: {
min: 0,
max: 7
}
});
knex.select("*").from("users").then(function(rows){
console.log(rows);
});
It works with no problems.
Thank you!
Related
Using ExpressJS, when I run my project using 'node server.js' command, I get the following error:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '../config/db.config.js'
Require stack:
/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js'
]
}
This is my code inside the db.js file:
const mysql = require("mysql");
var dbConfig = require("../config/db.config.js");
// Create a connection to the database
const connection = mysql.createConnection({
host: dbConfig.HOST,
user: dbConfig.USER,
password: dbConfig.PASSWORD,
database: dbConfig.DB
});
// open the MySQL connection
connection.connect(error => {
if (error) throw error;
console.log("Successfully connected to the database.");
});
module.exports = connection;
Here is the directory structure of the project.
I have also tried this path: './config/db.config.js' but I still get the same error.
Can someone please help?
I'm making an application with ORM sequelize for the first time, I got to the model part, but when I run it it points me to an error that I can't solve, if anyone knows or can help me, thank you in advance, I'll leave the model code below and the error message
const Model = require('sequelize');
const DataTypes = require('sequelize');
class User extends Model {
static init(sequelize) {
super.init({
name: DataTypes.STRING,
password: DataTypes.INTEGER,
email: DataTypes.STRING,
}, {
sequelize
})
}
}
module.exports = User;
this is the error message:
super.init({
^
TypeError: (intermediate value).init is not a function
at Function.init (/home/lagos/Documents/MyProjectSGBD/api/src/models/User.js:7:11)
at Object. (/home/lagos/Documents/MyProjectSGBD/api/src/database/index.js:8:6)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (/home/lagos/Documents/MyProjectSGBD/api/src/app.js:4:1)
[nodemon] app crashed - waiting for file changes before starting...
I think your imports are not correct. It should be
const { Sequelize, DataTypes, Model } = require('sequelize');
Also, why not define models with sequelize.define.
If you are defining them through extending Model, then try this
const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');
class User extends Model {}
User.init({
name: DataTypes.STRING,
password: DataTypes.INTEGER,
email: DataTypes.STRING,
},{
sequelize
});
console.log(User === sequelize.models.User); // true
Reference -> https://sequelize.org/master/manual/model-basics.html
I´m trying to connect my app to MySql Database using require.js. But am getting following error :
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
my server side code look like this :
mijs.js
`require(["mysql"],function(mysql) {
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: 'XXXX',
database: 'XXXXXXX',
port: 3306
});
connection.connect(function(error){
if(error){
throw error;
}else{
console.log('Conexion correcta.');
}
});
});`
And the error message is :
internal/validators.js:112
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received type object
←[90m at validateString (internal/validators.js:112:11)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:831:3)←[39m
←[90m at require (internal/modules/cjs/helpers.js:74:18)←[39m
at Object.<anonymous> (C:\xampp\htdocs\Middleware\Temas\mijs.js:3:1)
←[90m at Module._compile (internal/modules/cjs/loader.js:945:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:798:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:711:12)←[39m
←[90m at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)←[39m
←[90m at internal/main/run_main_module.js:17:11←[39m
I have been checking for the connectivity of MariaDB, with Sequelize.
const Sequelize = require('sequelize');
// Setting up database (MariaDB) connection
const sequelize = new Sequelize('dbName', 'usr', 'pass', {
host: 'localhost',
dialect: 'mariadb'
});
But I am getting the following error:
/home/lt-196/api/node_modules/sequelize/lib/sequelize.js:236
throw new Error('The dialect ' + this.getDialect() + ' is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.');
^
Error: The dialect mariadb is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.
at new Sequelize (/home/lt-196/api/node_modules/sequelize/lib/sequelize.js:236:15)
at Object.<anonymous> (/home/lt-196/api/app.js:21:19)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
MariaDB
For MariaDB compatibility you have to install the package mariasql#0.1.20, or higher.
The configuration needs to look like this:
var sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'mariadb'
})
Or Try this:
MariaSQL: https://www.npmjs.com/package/mariasql
A node.js binding to MariaDB's non-blocking (MySQL-compatible) client library.
var Client = require('mariasql');
var c = new Client({
host: '127.0.0.1',
user: 'foo',
password: 'bar'
});
c.query('SHOW DATABASES', function(err, rows) {
if (err)
throw err;
console.dir(rows);
});
c.end();
MariaSQL is recommended.
https://github.com/MariaDB/mariadb-connector-nodejs
NPM
npm install --save mariadb
npm install --save sequelize#next
Yarn
yarn add mariadb
yarn add sequelize#next
const Sequelize = require('sequelize'),
sequelize = new Sequelize(process.env.db_name, process.env.db_user, process.env.db_pass, {
dialect: 'mariadb',
dialectOptions: {
socketPath: process.env.db_socket,
timezone: process.env.db_timezone
},
pool: {
min: 0,
max: 5,
idle: 10000
},
define: {
charset: 'utf8',
timestamps: false
},
benchmark: false,
logging: false
})
i'm trying to run my project on centos 7 OS, i'm trying to install my project by writing node install_server.js
and the terminal returns me an error
install_server.js:26
fs.readFile(`${__dirname}/config/config.sql`, (err, data) => {
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
And my source code of this file is found below
const cfg = require('./config/config.json');
const knex = require('knex');
const fs = require('fs');
const mysql = require('mysql');
var db = knex({
client: 'mysql',
pool: {
min: 0,
max: 1
},
connection: {
host: cfg.database.db_server,
user: cfg.database.db_user,
password: cfg.database.db_password
}
});
var myCon = mysql.createConnection({
multipleStatements: true,
host: cfg.database.db_server,
user: cfg.database.db_user,
password: cfg.database.db_password
});
fs.readFile(`${__dirname}/config/config.sql`, (err, data) => {
if (err)
throw err;
myCon.query(data.toString(), function(err, results) {
if (err)
throw err;
console.log('complete')
});
});
Cant seem to figure it out, could i get a helphand?
Template Literals are not supported until version 4.8.4 according to
http://node.green/
which is a great resource.