为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));
});
答案 0 :(得分:0)
根据文档,你想要这样的东西。
user.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);
});
});