简体   繁体   English

module.exports不是nodejs函数

[英]module.exports is not a function nodejs

Quoting only the important parts of the code. 仅引用代码的重要部分。 This is my server.js in the node backend server 这是我在节点后端服务器中的server.js

//routes
const userapi = require('./api/routes/user');
//models
require('./api/models/userModel');

//use routes
userapi(app);

on my userapi which is my routes I call my userController 在我的userapi ,这是我的路线,我称我的userController

(function () {
    'use strict';

    module.exports = function (app) {
        var userCtrl = require('../controllers/userCtrl');

        app.post('/api/validate_user', function(req, res, next){
            userCtrl.validate_user(req, res);
        });
    }
})();

and this is my userController code. 这是我的userController代码。 In the future, i will write a code that will handle all the transaction from my MongoDB. 将来,我将编写一个代码来处理MongoDB中的所有事务。

'use strict';

var mongoose = require('mongoose'),
    model = mongoose.model('users'),
    brcpyt = require('bcrypt');

module.exports = function() {
    return {

        validate_user: function(req, res) {

            console.log('here');
        }
    }
};

The problem is, everytime I make a http request from my frontend. 问题是,每次我从前端发出http请求时。 I get an error that says userCtrl.validate_user is not a function. 我收到一条错误消息,指出userCtrl.validate_user不是一个函数。 My frontend can access the endpoints of '/api/validate_user' and the var userCtrl = require('../controllers/userCtrl') is a correct file path, but everytime that I type userCtrl to my routes it does not give me function validate_user 我的前端可以访问'/api/validate_user'的终结点,并且var userCtrl = require('../controllers/userCtrl')是正确的文件路径,但是每次我将userCtrl到路由时,它都不会给我功能validate_user

validate_user不在列表中

它位于

require gives you the value assigned to module.exports, so your code is like: require给您分配给module.exports的值,因此您的代码如下:

var userCtrl = function() {
    return {

        validate_user: function(req, res) {

            console.log('here');
        }
    }
};

userCtrl.validate_user(...);

Do you see the bug now? 您现在看到错误了吗? userCtrl is a function, and therefore doesn't have a validate_user property ... userCtrl是一个函数,因此不具有validate_user属性。

You really don't need to be wrapping your modules in IIFEs. 您确实不需要将模块包装在IIFE中。 That said, the issue you are seeing specifically is because your userCtrl module is exporting a function that, when called, returns an object which will then have a validate_user member -- it's not exporting an object directly. 也就是说,您具体看到的问题是因为您的userCtrl模块正在导出一个函数,该函数在被调用时返回一个对象,该对象将具有validate_user成员-而不是直接导出对象。 Try checking the value of typeof userCtrl - it'll be function instead of an object , which is what you're expecting here :) 尝试检查typeof userCtrl的值-它将是function而不是object ,这是您期望的在这里:)

Specifically, the following should work with no changes to userCtrl: 具体来说,以下内容应在不更改userCtrl的情况下起作用:

var userCtrl = require('../controllers/userCtrl')()

Notice the extra parenthesis at the end? 注意最后的括号吗? That will invoke the exported function and give you back an object with a validate_user method. 这将调用导出的函数,并为您提供一个带有validate_user方法的对象。 This pattern is sometimes used when you'd like to pass some options to the module, like a factory pattern. 当您要将某些选项传递给模块时,有时会使用此模式,例如工厂模式。 For example, 例如,

const userCtrlFactory = require('../controllers/userCtrl') const userCtrl = userCtrlFactory({ option: value })

This, however, is not the best way to do so, you would be using classes for that. 但是,这不是最好的方法,您将为此使用类。

I recommend you drop the IIFEs from your code ( (function () {})() ) and just have a top-level module.exports (like you did with the userCtrl) that exports either an object or a class, depending on what you need from it. 我建议您从代码( (function () {})() )中删除IIFE,并只使用一个顶层module.exports (就像您对userCtrl所做的那样)会根据对象导出一个对象或一个类。您需要它。 For userCtrl, just export an object directly as you're not passing options or have a need for "instantiating" a controller more than once: 对于userCtrl,只需直接导出对象即可,因为您没有传递选项,或者需要多次“实例化”控制器:

'use strict'; module.exports = { validate_user: ... };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM