我的标准代码如下:
module.exports = function(){
'use strict';
return {
getAllUsers : getAllUsers,
getWatchlists : getWatchlists,
getUserBidDetails : getUserBidDetails,
addToWatchlist : addToWatchlist,
removeFromWatchlist : removeFromWatchlist,
getUserBuyingLimit : getUserBuyingLimit,
userBidDetails : userBidDetails,
getUserWatchlists : getUserWatchlists
};
}
我已经定义了我们在module.exports中返回的所有函数,但是当最后一个函数,即" getUserWatchlists"得到调用然后它抛出错误
Error: has no method 'getUserWatchlists'
我可能没有使用正确的方法在单个module.exports函数中返回多个函数。请建议
答案 0 :(得分:5)
为什么不将module.exports
设置为您当前返回的对象?例如:
module.exports = {
getAllUsers: getAllUsers,
getWatchlists: getWatchlists,
getUserBidDetails: getUserBidDetails,
addToWatchlist: addToWatchlist,
removeFromWatchlist: removeFromWatchlist,
getUserBuyingLimit: getUserBuyingLimit,
userBidDetails: userBidDetails,
getUserWatchlists: getUserWatchlists
};
或者,如果您想避免重复这样的名称,您可以只在exports
对象上设置函数,并避免重新分配module.exports
。例如:
exports.getAllUsers = function getAllUsers(...) {
// ...
};
exports.getWatchlists = function getWatchlists(...) {
// ...
};
// ... etc.