Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

नोड के साथ सरल उपयोगकर्ता लॉगिन सत्यापन मॉड्यूल

मुझे लगता है कि आप अपने ऐप को अधिक नोड-जैसे तरीके से पुनर्विचार करना चाहेंगे (यानी वह जो पहचानता है कि कई/अधिकांश चीजें असीमित रूप से होती हैं, इसलिए आप आमतौर पर इस तरह के फ़ंक्शन से "वापसी" नहीं कर रहे हैं, लेकिन से कॉलबैक कर रहे हैं यह सुनिश्चित नहीं है कि आप नोड-mysql से क्या प्राप्त करने की योजना बना रहे हैं, लेकिन मैं शायद केवल सादे MySQL मॉड्यूल का उपयोग करूंगा। निम्न कोड अभी भी पूरी तरह से वह नहीं है जो आप चाहते हैं, लेकिन उम्मीद है कि आप इसके बारे में सही ढंग से सोचेंगे।

ध्यान दें कि नीचे 'रिटर्न' का उपयोग वास्तव में परिणाम नहीं लौटा रहा है (कॉलबैक स्वयं कुछ भी वापस नहीं करना चाहिए, और इस प्रकार यह अपरिभाषित लौटने जैसा है। रिटर्न स्टेटमेंट वहां हैं इसलिए आप फ़ंक्शन से बाहर निकलते हैं, जो बहुत कठिन बचाता है अगर / अन्य ब्लॉक।

आशा है कि यह मदद करता है, लेकिन मैं सुझाव दूंगा कि नोड के लिए लेखन की अतुल्यकालिक प्रकृति के लिए बेहतर अनुभव प्राप्त करने के लिए जीथब पर विभिन्न नोड परियोजनाओं को देखें।

function validate(username, password, callback){
    var connection = mysql.createConnection({ user:'foo',
                            password: 'bar',
                            database: 'test',
                            host:'127.0.0.1'});

    connection.connect(function (err){
        if (err) return callback(new Error('Failed to connect'), null);
        // if no error, you can do things now.

        connection.query('select username,password from usertable where username=?',
                username,
                function(err,rows,fields) {
                    //  we are done with the connection at this point), so can close it
                    connection.end();

                    // here is where you process results
                    if (err)
                        return callback(new Error ('Error while performing query'), null);
                    if (rows.length !== 1)
                        return callback(new Error ('Failed to find exactly one user'), null);

                    // test the password you provided against the one in the DB.
                    // note this is terrible practice - you should not store in the
                    // passwords in the clear, obviously. You should store a hash,
                    // but this is trying to get you on the right general path

                    if (rows[0].password === password) {
                        // you would probably want a more useful callback result than 
                        // just returning the username, but again - an example
                        return callback(null, rows[0].username);
                    } else {
                        return callback(new Error ('Bad Password'), null);
                    }

                });


    });
};



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. कॉलम में से किसी एक द्वारा MySQL तालिका को त्वरित रूप से पुन:क्रमबद्ध कैसे करें?

  2. Xamarin ऑफ़लाइन सिंक के लिए Azure में MySQL बैक-एंड सेट करें

  3. MySQL DECLARE में वेरिएबल में चयन करें सिंटैक्स त्रुटि का कारण बनता है?

  4. PHP और MySQL का उपयोग करके 'क्विज़' वेब एप्लिकेशन विकसित करने के लिए डेटाबेस डिज़ाइन

  5. MySQL में यादृच्छिक पंक्ति