【问题标题】:Module name is not defined - node js未定义模块名称 - node js
【发布时间】:2015-08-09 12:48:51
【问题描述】:

我正在尝试让“async”模块(not async/await)在 nodejs 上运行,但出现以下错误:

ReferenceError: 未定义异步 在 D:\project\routes\index.js:58:2 在 Layer.handle [as handle_request] (D:\project\node_modules\express\lib\router\layer.js:82:5) 在下一个 (D:\project\node_modules\express\lib\router\route.js:110:13) 在 Route.dispatch (D:\project\node_modules\express\lib\router\route.js:91:3) 在 Layer.handle [as handle_request] (D:\project\node_modules\express\lib\router\layer.js:82:5) 在 D:\project\node_modules\express\lib\router\index.js:267:22 在 Function.proto.process_params (D:\project\node_modules\express\lib\router\index.js:321:12) 在下一个 (D:\project\node_modules\express\lib\router\index.js:261:10) 在 Function.proto.handle (D:\project\node_modules\express\lib\router\index.js:166:3) 在路由器 (D:\project\node_modules\express\lib\router\index.js:35:12)

server.js 文件中我添加了:var async = require('async'); 我使用 async.series 的代码:

/* POST to Add User Service */
router.post('/register', function(req, res) {    
var error = false;
var errors = [];

// Set our internal DB variable
var db = req.db;

// Get our form values. These rely on the "name" attributes
var user_email = req.body.useremail;
var user_password = req.body.userpassword;
var user_repeat_password = req.body.repeatpassword;

var encrypted_password;
var ip;

async.series([
    function(callback){
        // Check if entered passwords matches
        if(user_password !== user_repeat_password){
            error = true;
            errors.push('Password does not match');
        }
        if(user_password.length < 6){
            error = true;
            errors.push('Password to short, must be minimal 6 characters long');
        }
        callback();
    },
    function(callback){
        // Encrypt password
        var salt = "test";
        var salt2 = "test2";
        encrypted_password = SHA512(SHA512(SHA512(salt.concat(user_password,salt2))));

        // Get IP
        ip = req.ip;
        if(ip==""){
            error = true;
            errors.push('No IP found?');
        }

        // Validate email
        function validateEmail(user_email) {
            var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
            return re.test(user_email);
        }
        if (!validateEmail(user_email)) {
            error = true;
            errors.push('Unvalid email');
        }
        else{
            // Check if email already exists
            var collection = db.get('users');
            collection.find({'email':user_email}, function(err, items){
                if(items == ""){
                    console.log('Email available');
                }
                else{
                    error = true;
                    errors.push('Email already in use');
                    console.log('Email UNavailable');
                }
            }, function (err, doc) {
                // If it failed, return error
                res.send("An error occurred while processing the data, please contact the site administrator.");
            });
        }
        callback();
    }
], function(err){

    console.log(error);

    // Check if errors
    if(error == true){
        console.log('error = true');
        errors.push("<a href='/register'>Try Again</a>");
        var error_string = errors.join("<br>");
        res.send(error_string);
    }
    else{
        console.log('error = false');

        // Set our collection
        var collection = db.get('users');

        // Submit to the DB
        collection.insert({
            "email" : user_email,
            "password" : encrypted_password,
            "status" : false,
            "ip" : ip
        }, function (err, doc) {
            if (err) {
                // If it failed, return error
                res.send("An error occurred while processing the data, please contact the site administrator.");
            }
            else {
                // If it worked, set the header so the address bar doesn't still say /adduser
                res.location("userlist");
                // And forward to success page
                res.redirect("userlist");
            }
        });
    }
});
});

【问题讨论】:

  • 这段代码是否在同一个文件中,server.js
  • 需要你使用它的模块。
  • 不,它在 index.js 文件中。 server.js 需要的: var routes = require('./routes/index');

标签: javascript node.js asynchronous


【解决方案1】:

您需要在将要使用它的同一文件中要求 Async 模块。

var async = require('async');

async.series(...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2018-01-27
    • 2018-12-28
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多