Search code examples
mysqlnode.jsnode-orm2

Creating a new model doesn't work - no error


i am using node-orm2 with mysql in my project. I have already created the database tables, and i can query/find data in my DB. However, when i want to insert new data, nothing happens - no error in the callback, nothing.

Here is the relevant code:

Model class:

module.exports = function (orm, db) {
    var Comment = db.define('comment', {
        body: {type: 'text'}
    });
};

Index.js in the model folder:

var orm      = require('orm');
var settings = require('../config/settings');

var connection = null;

function setup(db, cb) {
    require('./comment')(orm, db);

    return cb(null, db);
}

module.exports = function (cb) {
    if (connection) return cb(null, connection);

    orm.connect(settings.database, function (err, db) {
        if (err) return cb(err);

        connection = db;
        db.settings.set('instance.returnAllErrors', true);
        db.settings.set('connection.debug', true);
        setup(db, cb);
    });
};

Controller:

var orm     = require('orm');

exports.create = function(req, res){
    var testcomment = {};
    testcomment.body = "test comment";
    req.models.comment.create(testcomment, function (err, message) {
            if (err) return console.log(err);
            return res.send(200, message);
        });
};

Environment.js

var path = require('path');
var express = require('express');
var settings = require('./settings');
var models = require('../models/');
var logger = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

module.exports = function (app) {
    app.use(express.static(path.join(settings.path, 'public')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));
    app.use(methodOverride());
    app.use(function (req, res, next) {
        models(function (err, db) {
            if (err) return next(err);

            req.models = db.models;
            req.db = db;

            return next();
        });
    })
};

Settings.js:

var path       = require('path');

var settings = {
    path       : path.normalize(path.join(__dirname, '..')),
    port       : process.env.NODE_PORT || 3001,
    database   : {
        protocol : "mysql",
        host     : "localhost",
        port     : "3306",
        database : "taxidatabase",
        user     : "root",
        password : "admin"
    }
};

module.exports = settings;

I basically followed the pattern in the example application in node-orm2 - but it doesn't work. Any idea, why?

Thanks!


Solution

  • Before adding anything to table you need to sync the DB at least once after you define the models in order to create the tables:

    var models = require('../app/models/');
    
    models(function (err, db) {
      if (err) throw err;
      db.sync(function (err) {
        if (err) throw err;
        console.log('Done!');
      });
    });
    

    Or maybe syncing the comment model will do:

    var orm     = require('orm');
    
    exports.create = function(req, res){
        var testcomment = {};
        testcomment.body = "test comment";
        req.models.comment.create(testcomment, function (err, message) {
            if (err) return console.log(err);
            return res.send(200, message);
        });
    
        req.models.comment.sync(function (err) {
          if (err) throw err;
          console.log('Done!');
      });
    };