mysql - 创建新模型不起作用 - 没有错误

标签 mysql node.js node-orm2

我在我的项目中使用 node-orm2 和 mysql。我已经创建了数据库表,我可以在我的数据库中查询/查找数据。然而,当我想插入新数据时,什么也没有发生——回调中没有错误,什么也没有。

相关代码如下:

模型类:

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

模型文件夹中的 Index.js:

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);
        });
};

环境.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();
        });
    })
};

设置.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;

我基本上遵循了 node-orm2 示例应用程序中的模式 - 但它不起作用。有什么想法,为什么?

谢谢!

最佳答案

在向表中添加任何内容之前,您需要在定义模型之后至少同步 DB 一次以创建表:

var models = require('../app/models/');

models(function (err, db) {
  if (err) throw err;
  db.sync(function (err) {
    if (err) throw err;
    console.log('Done!');
  });
});

或者同步评论模型可以:

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!');
  });
};

关于mysql - 创建新模型不起作用 - 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127446/

相关文章:

V8 上的 JavaScript 测试

javascript - 如何根据IP位置将用户重定向到子域?

Node.js orm2 - 在 OneToMany 关系中检索新关联时遇到问题

Mysql请求-获取联系人

mysql - 获取自动递增整数的最接近的总和

node.js - 如何存储第三方凭据(无 api、无 OAuth)以供自动重用?

mysql - LEFT/RIGHT 在 node-orm2 上连接

node.js - 使用 Knockout.js 和 ORM 的 TypeScript

树莓派 3 : i wasn't able to write a value in db using a variable 上的 python 和 sql

mysql - 我可以有条件地强制执行唯一性约束吗?