node-sqlagent icon indicating copy to clipboard operation
node-sqlagent copied to clipboard

500: Internal Server ErrorTypeError: this.errors.clear is not a function

Open blaasvaer opened this issue 8 years ago • 10 comments

When I have this, according to the documentation, in definitions/mysql.js:

NOTE: the documentation even states, that I should also have the var sql = ... in the definitions file ... but this seems wrong, as I would have no way of accessing this from the model then (yep, total confusion).

const MYSQL_CONNECTION_STRING = "Server=localhost;Port=80;Database=my_database;Uid=root;Pwd=root";
require('sqlagent/mysql').init( MYSQL_CONNECTION_STRING, true ); // debug is by default: false

And I access it from models/projects.js?

exports.getProjects = function ( next ) {
	let errorBuilder = new ErrorBuilder();
	let sql = DATABASE( errorBuilder );

	// Not doing anything with it yet
	// Just passing dummy data back to the view using `next( ... )`
}

I call this from a controller, like: NOTE: I pass dummy data to the view for it not to timeout.

function view_projects () {
	MODEL('projects').getProjects(function ( rows ) { // This is just dummy data for debugging
		self.view('projects', {
			projects : rows
		});
	});
}

I get this error:

500: Internal Server ErrorTypeError: this.errors.clear is not a function

What am I missing in the docs? Or is this simply just plain broken?

How do I set this up, so that I can actually define the DB Agent in a definitions file and call the database from a model?

blaasvaer avatar Mar 22 '18 13:03 blaasvaer

Hi, it's probably a bit misleading in the docs but you don't need var sql = ... in the definition file.

This goes into def file:

const MYSQL_CONNECTION_STRING = "....";
require('sqlagent/mysql').init( MYSQL_CONNECTION_STRING, true );

and this is how to use it anywhere in your app:

var sql = DATABASE( new ErrorBuilder() );

Regarding the error, can you show the whole error stack? What version of Total.js framework do you have?

molda avatar Mar 22 '18 14:03 molda

Version: v2.9.4

This is what it says:

======= 2018-03-22 14:57:34: default ---> TypeError: this.errors.clear is not a function (http://localhost:8000/projects) TypeError: this.errors.clear is not a function
    at Agent.clear (/Applications/MAMP/htdocs/timetracker/node_modules/sqlagent/mysql.js:703:15)
    at new Agent (/Applications/MAMP/htdocs/timetracker/node_modules/sqlagent/mysql.js:594:7)
    at Framework.framework.database (/Applications/MAMP/htdocs/timetracker/node_modules/sqlagent/mysql.js:1785:10)
    at Object.exports.getProject (/Applications/MAMP/htdocs/timetracker/models/projects.js:58:4)
    at Controller.view_projects (/Applications/MAMP/htdocs/timetracker/controllers/default.js:58:21)
    at IncomingMessage.PROTO.$total_execute2 (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14082:32)
    at IncomingMessage.PROTO.$total_execute (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14050:9)
    at IncomingMessage.PROTO.$total_prepare (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14342:9)
    at IncomingMessage.PROTO.$total_end (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:13991:9)
    at Framework.F.$requestcontinue (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:7055:9)

blaasvaer avatar Mar 22 '18 15:03 blaasvaer

If I remove all the DB stuff and just call the model, and only return the dummy data, I get:

500: Internal Server ErrorTypeError: name.substring is not a function
    at Framework.global.NOSQL.F.nosql (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:1158:16)
    at Framework.F.database (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:1150:11)
    at Object.exports.getProject (/Applications/MAMP/htdocs/timetracker/models/projects.js:57:4)
    at Controller.view_projects (/Applications/MAMP/htdocs/timetracker/controllers/default.js:58:21)
    at IncomingMessage.PROTO.$total_execute2 (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14082:32)
    at IncomingMessage.PROTO.$total_execute (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14050:9)
    at IncomingMessage.PROTO.$total_prepare (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:14342:9)
    at IncomingMessage.PROTO.$total_end (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:13991:9)
    at Framework.F.$requestcontinue (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:7055:9)
    at Server.F.listener (/Applications/MAMP/htdocs/timetracker/node_modules/total.js/index.js:6904:5)

blaasvaer avatar Mar 22 '18 15:03 blaasvaer

It's as if something somewhere expect something to be nosql ... I don't know.

blaasvaer avatar Mar 22 '18 15:03 blaasvaer

What would be nice, would be a complete example of how to set this up in an MVC fashion. You know, you define the sql agent, a controller calls a model, the model actually use the sql agent to access the database, and then return the data to the controller, which then sends it to the view ...

blaasvaer avatar Mar 22 '18 15:03 blaasvaer

The ONLY way I can get access to working with the DB is with this:

let mysql = require('mysql'),
	pool = mysql.createPool({
		host: 'localhost',
		user: 'root',
		password: 'root',
		database: 'timetracker',
		typeCast: function castField( field, useDefaultTypeCasting ) {
			if ( ( field.type === "BIT" ) && ( field.length === 1 ) ) {
			var bytes = field.buffer();
				return( bytes[ 0 ] === 1 );
			}
			return( useDefaultTypeCasting() );
		}
	});

F.database = function ( callback ) {
	return pool.getConnection( callback );
};

The SQL Agent seems kind of out of reach for me at the moment.

blaasvaer avatar Mar 22 '18 16:03 blaasvaer

Hi, I've tested it and i get the same error when i pass something else then errorbuilder to the DATABASE function. This causes the error you get let sql = DATABASE({});

Can you test this code for me?

Create new folder with index.js file and paste in this code:

require('total.js').http('debug');

const MYSQL_CONNECTION_STRING = "Server=localhost;Port=80;Database=my_database;Uid=root;Pwd=root";
require('sqlagent/mysql').init( MYSQL_CONNECTION_STRING, true );


setTimeout(function(){
    console.log('init');
    let errorBuilder = new ErrorBuilder();
    let sql = DATABASE( errorBuilder ); 
    // passing anything else then errorbilder instance will cause the error
}, 2000);

It should not throw any error.

Are you sure there's no other code in your app that somehow affect ErrorBuilder or Database?

The other error If I remove all the DB stuff... that's actualy OK since DATABASE function expects string by default. So if you don't use SQLAgent the DATABASE function works with embedded nosql db (flat file database engine built into the framework) but when you use SQLAgent the require('sqlagent/mysql').init(...) function will overwrite it.

molda avatar Mar 23 '18 13:03 molda

Hi @molda - I've created this little test project, trying to use code from another project that actually works. But for some reason I cannot make work on this simple test project ... and I also tried the above, but all it throws me is errors. Please, have a look at it and see if you can see where I'm going wrong all the time, as I can't seem to get my head around the inner workings of this framework ...

testing_db_stuff.zip

blaasvaer avatar Mar 28 '18 22:03 blaasvaer

Forgot to add the DB ... in case you need it for testing. Simple stuff.

timetracker_db.sql.zip

blaasvaer avatar Mar 28 '18 22:03 blaasvaer

Hi so i checked your code made a few changes and it works.

Most important : this doesn't work -> Server=localhost;Port=80;Database=my_database;Uid=root;Pwd=root

you need to use -> mysql://root:root@localhost:80/my_database

By default mysql runs on port 3306, are you sure 80 is correct?

Anyways, try this test.zip

molda avatar Mar 29 '18 09:03 molda