0

在 user.js 中为用户创建的模型

var orm = require("orm");

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

    var user = db.define("axi_users", {
        name           : String,
        id             : Number,
        enterprise_id  : Number,
        location_id    :  Number, 
        email          : String,
        pin            : Number,
        job_description: String,
        password        :String,
        role            :Number,
        designation     :String,
        department_id   :Number,
        created         :String,    
        updated         :String,
 });
 module.exports.user=user;   
 });

现在我想在 patu.js 中使用该用户模型对象,但它不允许在用户对象上使用方法。那么如何在节点 orm 中创建的其他文件中使用模型对象。以下文件是 patu.js。

var orm = require('orm');
var user= require('./user.js');

exports.list = function(req, res){
  res.send('respond with a resource');
};


 user.find({ id: 2 }, function (err, people) {     //VIEW BY ID
         if(err)
           {
             console.log(err);
              return;
              }

                 console.log("asad");


          console.log(JSON.stringify(people));            

    });     
4

1 回答 1

0

按照文档,你想要这样的东西。

用户.js

module.exports = function(db, callback) {
    db.define("axi_users", {
        name: String,
        id: Number,
        enterprise_id: Number,
        location_id: Number,
        email: String,
        pin: Number,
        job_description: String,
        password: String,
        role: Number,
        designation: String,
        department_id: Number,
        created: String,
        updated: String,
    });

    return callback();
}

patu.js

var orm = require("orm");
orm.connect("mysql://root:@localhost/axi_timescape", function(err, db) {
    if (err) throw err;

    db.load('./user.js', function(err) {
        if (err) throw err;

        var user = db.models.axi_users;

        user.find({
            id: 2
        }, function(err, people) { //VIEW BY ID
            if (err) {
                console.log(err);
                return;
            }

            console.log("asad");


            console.log(JSON.stringify(people));

        });
    });

});

或者,如果您使用的是快递。

var express = require('express');
var orm = require('orm');
var app = express();

app.use(orm.express("mysql://root:@localhost/axi_timescape", {
    define: function(db, models) {
        models.user = db.define("axi_users", {
            name: String,
            id: Number,
            enterprise_id: Number,
            location_id: Number,
            email: String,
            pin: Number,
            job_description: String,
            password: String,
            role: Number,
            designation: String,
            department_id: Number,
            created: String,
            updated: String,
        });
    }
}));
app.listen(80);

app.get("/", function(req, res) {
    req.models.user.find({
        id: 2
    }, function(err, people) { //VIEW BY ID
        if (err) {
            console.log(err);
            return;
        }
        res.send(people);
    });
});
于 2014-06-25T09:57:09.143 回答