我在 Nodejs Express 应用程序中使用mysql-orm这是我的代码:
应用程序.js:
// ... all variables are initialized
var orm = null;
var app = express();
db.create(dbSchema, dbSeeds, dbOptions, function(err, ormObject) {
if (err) throw err;
orm = ormObject;
});
var coursesRoutes = require('./routes/dashboard/courses')(orm);
app.use('/dashboard/courses', coursesRoutes);
/routes/dashboard/courses.js:
var express = require('express');
var router = express.Router();
module.exports = function(orm) {
router.get('/listall', function(req, res, next) {
orm.loadMany('sections', null, function(err, sections) {
if (err) throw err;
res.jsonp(sections);
});
});
return router;
};
这将返回此错误:
TypeError:无法读取 null 的属性“loadMany”
我知道问题是orm
变量在回调中初始化之前被传递给文件,db.create
但我不知道如何解决这个问题。
我试着说:
var coursesRoutes = require('./routes/dashboard/courses')(orm);
app.use('/dashboard/courses', coursesRoutes);
在回调中,但它返回 404 not found