使用orm2连接mysql的时候出错 ORMError: Connection protocol not supported
发布于 9 年前 作者 wp3xpp 5805 次浏览 最后一次编辑是 8 年前 来自 问答

前提是我装了mysql Ver 14.14 Distrib 5.1.68, 测试代码用的是官方的:

var orm = require("orm");

orm.connect("mysql://root:root@127.0.0.1/orm", function (err, db) {
  if (err) throw err;

    var Person = db.define("person", {
        name      : String,
        surname   : String,
        age       : Number, // FLOAT
        male      : Boolean,
        continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type
        photo     : Buffer, // BLOB/BINARY
        data      : Object // JSON encoded
    }, {
        methods: {
            fullName: function () {
                return this.name + ' ' + this.surname;
            }
        },
        validations: {
            age: orm.enforce.ranges.number(18, undefined, "under-age")
        }
    });

    // add the table to the database
    db.sync(function(err) { 
        if (err) throw err;

        // add a row to the person table
        Person.create({ id: 1, name: "John", surname: "Doe", age: 27 }, function(err) {
            if (err) throw err;

                // query the person table by surname
                Person.find({ surname: "Doe" }, function (err, people) {
                    // SQL: "SELECT * FROM person WHERE surname = 'Doe'"
                    if (err) throw err;

                    console.log("People found: %d", people.length);
                    console.log("First person: %s, age %d", people[0].fullName(), people[0].age);

                    people[0].age = 16;
                    people[0].save(function (err) {
                        // err.msg = "under-age";
                });
            });

        });
    });
});

有人知道怎么解决吗?

2 回复

看上去好像没什么错。 mysql的第三方包装了没。 还有检查下你的连接字符串,是否都正确,和你的数据库匹不匹配。 可以参考下 http://www.cnblogs.com/showtime813/p/4552086.html

npm link mysql 一下就好了,这个感觉有点坑啊 谢谢shadow88sky

回到顶部