私はAWS Lambdaサービスでホストされるいくつかのスクリプトに取り組んでいます。私はJSと大丈夫だからNode.jsを選んだが、PythonやJavaを学んだことはない。しかし、私はMySQLデータベースを照会する必要があるので、悪夢であることが証明されており、関数から結果を正しく取得する方法を理解できません。AWS Lambda node.js非同期関数からデータを返す方法は?
だから基本的に私はこれ(私はいくつかのものをカットしましたが、あなたはアイデアを得るべきです)を持っています、そしてこれは私がしたいことのすべてです。私はMySQLデータベースを照会できるようにしたいのですが、答えが返ってきたときにそれを返すか、エラーがあればスローします。
var mysql = require("mysql"); //npm installed module "mysql"
var err = require("./errors.js"); //internally requires npm installed module "errors"
var main = function(event, context, callback){
try{
//Confidential
var data = null;
//Confidential
try{
data = databaseCommand("SELECT * FROM `<table>` WHERE <field> = <value>");
}
catch(e){
if(e instanceof err.Database)
//Return state that indicates "Internal server error".
else
throw e;
return
}
//Do maths on data
//Return "OK" and result
}
catch(e){
//Return "Unkown error"
}
};
var databaseCommand = function(cmdString){
if(typeof cmdString !== "string") throw new err.InputInvalidType({explanation: "'cmdString' is of type '" + typeof cmdString + "', expected type 'string'"});
var connection = mysql.createConnection({
host: process.env.db_host,
port: process.env.db_port || 3306,
user: process.env.db_username,
password: process.env.db_password,
database: process.env.db_database
});
var ret = {
error: null,
result: null
};
//I cut out the connection.connect() because it can be implied and I'm confused enough
connection.query(cmdString, function(error, rows){
if(error)
ret.error = error;
else
ret.result = rows;
});
connection.end();
if(ret.error)
throw new err.Database();
return ret.result;
};
しかし、これは動作しません、明らかにNode.jsのに精通している人のためのconnection.queryへの呼び出しは非同期なので、私のdatabaseCommand関数は常にnullを返します(と投げていないため、 )私の主な機能でエラーが発生します。
このような基本的な同期要求をどのように実行できるか理解してください。私は次の変更(私はおそらく、この間違ったを持っている)のようなものを示して非同期メソッドを使用しての「ソリューション」を見てきましたが、私は、これは何が違うのですか見ない
EDIT
。
var mysql = require("mysql"); //npm installed module "mysql"
var err = require("./errors.js"); //internally requires npm installed module "errors"
var main = function(event, context, callback){
try{
//Confidential
var data = null;
//Confidential
try{
databaseCommand("SELECT * FROM `<table>` WHERE <field> = <value>", function(err, result){
if(err)
throw err;
data = result;
});
//This function will still return before data is set
//Maths will still be performed before data is set
}
catch(e){
if(e instanceof err.Database)
//Return state that indicates "Internal server error".
else
throw e;
return
}
//Do maths on data
//Return result
}
catch(e){
//Return "Unkown error"
}
}
var databaseCommand = function(cmdString, callback){
if(typeof cmdString !== "string") throw new err.InputInvalidType({explanation: "'cmdString' is of type '" + typeof cmdString + "', expected type 'string'"});
var connection = mysql.createConnection({
host: process.env.db_host,
port: process.env.db_port || 3306,
user: process.env.db_username,
password: process.env.db_password,
database: process.env.db_database
});
var ret = {
error: null,
result: null
};
//I cut out the connection.connect() because it can be implied and I'm confused enough
connection.query(cmdString, function(error, rows){
if(error)
callback(err, null);
else
callback(null, rows);
});
connection.end();
}
'行を返す;'や 'リターンret.result;'として前者は「 – bugwheels94
非同期かどうかをすることになっているかどうか、常に関係なく不定になりますreturn ret.result "、更新された質問を表示する...私の戻り値は未定義ではなく、" ret.result "が最初にヌルに設定されているので、常にnullになり、その値が設定される前に関数が戻ります。 – TheBeardedQuack
はあなたが呼び出しは同期になりたいですか、ノードjsの非同期動作を受け入れ、あなたのコードの動作を変更し、代わりに – bugwheels94