2017-03-04 34 views
6

से एक MySQL डेटाबेस क्वेरी करना मुझे अपने एडब्ल्यूएस लैम्ब्डा फ़ंक्शन के अंदर मेरे MySQL डेटाबेस (एडब्ल्यूएस से दूरस्थ रूप से होस्ट किया गया) पूछताछ में समस्या हो रही है।एक नोडजेएस एडब्ल्यूएस लैम्ब्डा फंक्शन

यह भागों मैं लैम्ब्डा समारोह के बाकी (जो एक एलेक्सा कौशल के लिए बुलाया जा रहा है) के लिए की जरूरत के अलावा मेरे कोड है:

var mysql = require('mysql'); 
    var connection = mysql.createConnection({ 
     host  : '<myserver>', 
     user  : '<myusername>', 
     password : '<mypw>', 
     database : '<mydatabase>' 
    }); 
    connection.connect(function(err){ 
     if(!err) { 
       console.log("Database is connected ... nn"); 
     } 
     else { 
       console.log("Error connecting database ... nn"); 
     } 
    }); 

    connection.query("INSERT INTO Users (user_id) VALUES ('TESTNAME')"); 
    connection.end(); 

यह सिर्फ ठीक काम करता है जब मैं से नोड के साथ इसे चलाने मेरी कमांड प्रॉम्प्ट:

node index.js 

मैं "mysql" मॉड्यूल index.js साथ निर्देशिका में NPM के माध्यम से स्थापित उपयोग कर रहा हूँ और यह ज़िप और मेरी लैम्ब्डा समारोह पर अपलोड करें।

फिर से, यह मेरी विकास मशीन पर काम करता है, लेकिन मेरे लैम्ब्डा फ़ंक्शन का परीक्षण करते समय कोई संकेतक नहीं देता है क्योंकि यह मेरे डेटाबेस को बिल्कुल प्रभावित नहीं करता है।

मेरा प्रश्न एलेक्सा और लैम्ब्डा में फैला है जितना कि यह mysql Node.JS मॉड्यूल का उचित उपयोग करता है।

यहां मेरे लैम्ब्डा के लिए मेरा वर्तमान कोड है, और यहां समस्या यह है कि अभी भी मेरा परीक्षण मूल्य -> ​​"TESTNAME" नामक उपयोगकर्ता नाम मेरे MySQL डेटाबेस में नहीं जोड़ा जाता है।

मैंने पहली कॉल टिप्पणी के रूप में क्वेरी कॉलबैक में क्वेरी डाली है, और मैं अपना पुराना कोड अपडेट करने के बजाय बस अपना नया कोड डाल रहा हूं, यह रिकॉर्ड रखने के लिए कि मुझे लगता है कि कोड में क्या होना चाहिए मेरी एलेक्सा के लैम्ब्डा समारोह:

अपडेट किया गया कोड:

var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
     host  : '<myserver>', 
     user  : '<myusername>', 
     password : '<mypw>', 
     database : '<mydatabase>' 
}); 
exports.handler = (event, context) => { 
    try { 

     if (event.session.new) { 
      // New Session 
      console.log("NEW SESSION"); 
     } 


     switch (event.request.type) { 

      case "LaunchRequest": 
       // Launch Request 
       console.log(`LAUNCH REQUEST`); 
       context.succeed(
        generateResponse({}, 
         buildSpeechletResponse("Welcome to an Alexa Skill, this is running on a deployed lamda function", true) 
        ) 
       ); 
       break; 

      case "IntentRequest": 
       // Intent Request 
       console.log(`Intent Request`); 
       console.log('Then run MySQL code:'); 
       connection.connect(function(err) { 
        console.log('Inside connection.connect() callback'); 
        if (!err) { 
         console.log("Database is connected ... "); 
         connection.query("INSERT INTO Users (user_id) VALUES ('TESTNAME')", 
          function(err, result) { 
           console.log("Inside connection.query() callback") 
           if (!err) { 
            console.log("Query Successful! Ending Connectection."); 
            connection.end(); 
           } else { 
            console.log("Query error!"); 
           } 
          }); 
        } else { 
         console.log("Error connecting database ..." + err.message); 
        } 
       }); 
       context.succeed(
        generateResponse({}, 
         buildSpeechletResponse("Welcome to the incredible intelligent MySQLable Alexa!", true) 
        ) 
       ); 

       break; 

      case "SessionEndedRequest": 
       // Session Ended Request 
       console.log(`SESSION ENDED REQUEST`); 
       break; 

      default: 
       context.fail(`INVALID REQUEST TYPE: ${event.request.type}`); 

     } 

    } catch (error) { 
     context.fail(`Exceptiodn: ${error}`) 
    } 

}; 

//Helpers 
buildSpeechletResponse = (outputText, shouldEndSession) => { 

    return { 
     outputSpeech: { 
      type: "PlainText", 
      text: outputText 
     }, 
     shouldEndSession: shouldEndSession 
    }; 
}; 

generateResponse = (sessionAttributes, speechletResponse) => { 
    return { 
     version: "1.0", 
     sessionAttributes: sessionAttributes, 
     response: speechletResponse 
    }; 
}; 

और मेरे कंसोल आउटपुट:

START RequestId: 5d4d17a7-0272-11e7-951c-b3d6944457e1 Version: $LATEST 
2017-03-06T13:39:47.561Z 5d4d17a7-0272-11e7-951c-b3d6944457e1 Intent Request 
2017-03-06T13:39:47.562Z 5d4d17a7-0272-11e7-951c-b3d6944457e1 Then run MySQL code: 
END RequestId: 5d4d17a7-0272-11e7-951c-b3d6944457e1 
REPORT RequestId: 5d4d17a7-0272-11e7-951c-b3d6944457e1 Duration: 82.48 ms Billed Duration: 100 ms  Memory Size: 128 MB Max Memory Used: 14 MB 
+0

आप कनेक्ट कॉलबैक अंदर क्वेरी चलाने की जानी चाहिए:

यहाँ मेरी कोड अब तक (एक उचित क्वेरी अब हो रहा हो रही) है। यहां, आप 'कनेक्ट()' का इलाज कर रहे हैं जैसे कि यह किसी भी तरह सिंक्रोनस था, जो यह नहीं है, इसलिए जब आप 'क्वेरी()' चलाते हैं तब भी आप कनेक्ट हो सकते हैं या नहीं भी हो सकते हैं यह आपके लिए दुर्घटना से स्थानीय रूप से काम करता है। आप किसी भी त्रुटि 'क्वेरी()' को निजीकृत कर सकते हैं। –

+0

क्या मुझे कॉलबैक के अंदर कनेक्शन भी समाप्त करना चाहिए? –

+0

मैंने अपने प्रश्न की पूरी सीमा के बारे में अधिक जानकारी के साथ अपना प्रश्न अपडेट किया और मुझे अपनी क्वेरी कॉलबैक में अपनी क्वेरी डालकर जो लगता है उसका जोड़ा। जैसे ही मैं इसे अपने MySQL डेटाबेस पर पोस्ट करने के लिए प्राप्त कर सकता हूं, मैं अपने कार्यों के लिए अपनी त्रुटि प्रबंधन का ख्याल रखूंगा, क्योंकि जहां तक ​​मुझे पता है कि मुझे लैम्ब्डा फ़ंक्शन का परीक्षण करते समय कंसोल आउटपुट नहीं मिलता है, तो यह होगा इस बिंदु पर थोड़ा असहज। –

उत्तर

3

समस्या यह है कि मैं अपने चोर डाल करने के लिए आवश्यक था मेरे कॉलबैक के अंदर text.succeed। एसकब्लोट के लिए बहुत धन्यवाद, क्योंकि कॉलबैक की उनकी बात ने मुझे अध्ययन करने का नेतृत्व किया जहां चीजें वास्तव में उनके निष्पादन को समाप्त कर रही थीं।

तो जाहिर है कि एडब्ल्यूएस लैम्ब्डा का उपयोग करते समय, यदि आपके कॉलबैक से पहले "संदर्भ" समाप्त होता है, तो आपको कॉलबैक नहीं मिलते हैं। तो भले ही मैंने अपने सभी कॉलबैक को इस प्रकार रखा था: कनेक्ट -> क्वेरी -> अंत, कनेक्ट से श्रृंखला का पहला कॉलबैक कभी भी नहीं कहा जाता है क्योंकि "context.succeed" को बाद में कॉल किया जा रहा था, जो निष्पादन समाप्त हो गया था।

var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
    ... 
}); 

exports.handler = (event, context) => { 
    try { 

     if (event.session.new) { 
      // New Session 
      console.log("NEW SESSION"); 
     } 


     switch (event.request.type) { 

      case "LaunchRequest": 
       // Launch Request 
       console.log(`LAUNCH REQUEST`); 
       context.succeed(
        generateResponse({}, 
         buildSpeechletResponse("Welcome to an Alexa Skill, this is running on a deployed lamda function", true) 
        ) 
       ); 
       break; 

      case "IntentRequest": 
       // Intent Request 
       console.log(`Intent Request`); 
       console.log('Then run MySQL code:'); 
       connection.connect(function(err) { 
        console.log('Inside connection.connect() callback'); 
        if (!err) { 
         console.log("Database is connected ... "); 
         connection.query("INSERT INTO Users (user_id) VALUES ('TESTNAME')", 
          function(err, result) { 
           console.log("Inside connection.query() callback") 
           if (!err) { 
            console.log("Query Successful! Ending Connection."); 
            connection.end(); 
           } else { 
            console.log("Query error!"); 
           } 
          }); 
        } else { 
         console.log("Error connecting database ..." + err.message); 
        } 
        context.succeed(
         generateResponse({}, 
          buildSpeechletResponse("Welcome to the incredible intelligent MySQLable Alexa!", true) 
         ) 
        ); 
       }); 

       break; 

      case "SessionEndedRequest": 
       // Session Ended Request 
       console.log(`SESSION ENDED REQUEST`); 
       break; 

      default: 
       context.fail(`INVALID REQUEST TYPE: ${event.request.type}`); 

     } 

    } catch (error) { 
     context.fail(`Exceptiodn: ${error}`) 
    } 

}; 

//Helpers 
buildSpeechletResponse = (outputText, shouldEndSession) => { 

    return { 
     outputSpeech: { 
      type: "PlainText", 
      text: outputText 
     }, 
     shouldEndSession: shouldEndSession 
    }; 
}; 

generateResponse = (sessionAttributes, speechletResponse) => { 
    return { 
     version: "1.0", 
     sessionAttributes: sessionAttributes, 
     response: speechletResponse 
    }; 
}; 
+0

आपको अनुकूलित स्लॉट से मूल्य कैसे प्राप्त हुआ? "Event.slots। "value के रूप में मान प्राप्त करने का प्रयास करते समय मुझे एक त्रुटि मिली। मैंने" event.request.type.slots.value "भी कोशिश की। – seleniumlover

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