有很多类似的问题,我知道,但是我的情况略有不同,我尝试了这些答案中描述的方法,但仍然不起作用。我知道,我一定错过了什么,但我不知道是什么。
我有这样的结构:
根--模型-- categories.js -- ... --路由-- main.js -- ...app.js ...
因此,在我的分类中,我有以下功能/结构:
/*
* Initial variable setting
*/
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server,
BSON = require('mongodb').BSON,
ObjectID = require('mongodb').ObjectID;
Categories = function(host, port) {
this.db = new Db('hoiepos', new Server(host, port, { safe: false }, { auto_reconnect: true }, {}));
this.db.open(function(){});
};
/*
* Get collection
*/
Categories.prototype.getCollection = function(callback) {
this.db.collection('categories', function(error, category_collection) {
if(error) callback(error);
else callback(null, category_collection);
});
};
...
exports.Categories = Categories;
显然,文件中还有其他函数,但为了简洁起见,我省略了它们。在我的路径文件中,我得到了:
var Categories = require('./models/categories');
exports.Melo = function(req, res) {
Categories.findAll(function(error, prds) {
res.send(prds);
});
};
在我的app.js中,我有:
var route = require('./routes').Melo;
app.get('/test', route);
然而,当我尝试启动Node时,我得到一个错误,说Node找不到模块'./models/categories‘。
值得注意的是,在models/和routes/中还有其他文件,即: /models/users、/models/orders、routes/users、routes/orders等。
我做错了什么?
请注意,我使用的不是Mongoose。
非常感谢你的帮助。
发布于 2013-12-12 13:56:19
如果Node找不到模块'./models/categories',那么您可能只是使用了错误的路径。如果您的项目结构如下:
根部
--模型
-Categories.js
--路由
-index.js
,则在路由文件中应使用var Categories = require('../models/categories');
https://stackoverflow.com/questions/20544021
复制