2015-05-27 9 views
6

मैं NodeJS पर काम कर async प्राप्त करने की कोशिश कर रहा हूँ, लेकिन मैं निम्नलिखित त्रुटि मिलती है:async परिभाषित किया गया है नहीं - नोड js

ReferenceError: async is not defined at D:\project\routes\index.js:58:2 at Layer.handle [as handle_request] (D:\project\node_modules\express\lib\router\layer.js:82:5) at next (D:\project\node_modules\express\lib\router\route.js:110:13) at Route.dispatch (D:\project\node_modules\express\lib\router\route.js:91:3) at Layer.handle [as handle_request] (D:\project\node_modules\express\lib\router\layer.js:82:5) at D:\project\node_modules\express\lib\router\index.js:267:22 at Function.proto.process_params (D:\project\node_modules\express\lib\router\index.js:321:12) at next (D:\project\node_modules\express\lib\router\index.js:261:10) at Function.proto.handle (D:\project\node_modules\express\lib\router\index.js:166:3) at router (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"); 
      } 
     }); 
    } 
}); 
}); 
+2

क्या यह कोड एक ही फ़ाइल में है, 'server.js'? –

+0

उस मॉड्यूल की आवश्यकता है जहां आप इसका उपयोग करते हैं। – Leo

+0

नहीं, यह index.js फ़ाइल में है। जो server.js द्वारा आवश्यक है: var मार्ग = आवश्यकता ('./ मार्ग/अनुक्रमणिका'); – user3004798

उत्तर

16

आप एक ही फाइल आप में इसका इस्तेमाल करने जा रहे हैं में Async मॉड्यूल की आवश्यकता की जरूरत है

। 10
+1

धन्यवाद, यह काम कर रहा है! – user3004798

संबंधित मुद्दे