我是 NodeJS 和 JS 的新手(主要是 PHP 和 C# 人),所以我真的可以在下面的这个函数中使用一些帮助。
目标是接收 JSON 负载,连接到 MySQL,然后在 JSON 响应中返回查询结果。我已经将它连接到数据库,我可以读取它接收到的 JSON 数据(event.fieldname),但由于某种原因它没有发回申请人_数据变量的 JSON。
我只是在错误的位置有变量吗?当我运行下面的代码时,我只是返回“{}”作为返回的数据。
在此先感谢您的帮助!
节点代码:
exports.handler = function(event, context, callback) {
console.log('Starting:');
console.log("Request received:\n", JSON.stringify(event));
var mysql = require('mysql');
var jsonconnection = mysql.createConnection({
host: 'servername',
user: 'username',
password: 'password',
database: 'database'
});
jsonconnection.connect();
console.log('Connected to MySQL:');
jsonconnection.query('SELECT applicant_id FROM customers WHERE applicant_id = \'' + event.applicant_id + '\'',
function(err,res){
if(err) throw err;
console.log('Row Details:', JSON.stringify(res));
var applicant_data = {
applicant_id : res.applicant_id
};
jsonconnection.end(); …Run Code Online (Sandbox Code Playgroud) 我是NodeJS函数调用的新手,现在我已经在屏幕上敲了几个小时,而我所有的谷歌搜索都没有帮助.
所以我所拥有的是一个AWS Lambda函数,它接收一个具有单个ID号的JSON对象.此ID号传递并最终作为myid发送到getJson函数.这部分正在运行,它正在使用NPM的REQUEST模块,它可以访问Web服务并撤回数据.当我在console.log(body)中看到我需要的JSON对象时.
问题是我无法让它返回数据,所以我可以在其他地方使用JSON.我尝试了CALLBACK(BODY),RETURN(BODY),但没有任何东西都能让我回复使用的数据.
我尝试在函数中使用回调函数,它确实调用了该函数,但即使该函数也不会返回数据供我使用.我已经将JSON硬编码为变量并返回它并且它可以工作......但是如果我使用REQUEST它就不会将它还给我.
我希望这很简单......非常感谢!
Calling the function:
query_result.success = 1;
query_result.message = "Applicant Data Found";
query_result.data = getJson(201609260000003, returningData);
function getJson(myid, callback){
request('http://server.com/service1.asmx/Get_Status_By_External_Id?externalId=' + myid + '',
function (error, response, body) {
console.log(body); // I see the JSON results in the console!!!
callback (body); // Nothing is returned.
}
);
}
function returningData(data){
console.log("ReturningData Function Being Called!");
var body = '{"Error":null,"Message":null,"Success":true,"ExternalId":"201609260000003","SentTimeStamp":"11/22/2016 1:07:36 PM","RespTimeStamp":"11/22/2016 1:08:32 PM","RespTot":"SRE"}';
return JSON.parse(body);
}
Run Code Online (Sandbox Code Playgroud) 我有三个正在使用的表。
我试图在单个查询中获取结果,该查询将从AccountingLine表返回所有行,并从Budget和Actuals表中求和每个AccountingLine的金额。
使用下面的SQL,SUM不适用于预算或实际数据。如果我删除其中一个联接和SUM函数之一,则它将为单个联接表正确计算。很奇怪...在MySQL的三个或更多表上有人通过多个SUM函数遇到这个问题吗?
SELECT A.*, SUM(B.`amount`) AS BudgetAmount, SUM(ACT.`amount`) as ActualAmount
FROM accounting_line A
LEFT JOIN budget B ON B.accounting_line_id = A.accounting_line_id
LEFT JOIN actual ACT ON ACT.accounting_line_id = A.accounting_line_id
GROUP BY A.`accounting_line_id`
Run Code Online (Sandbox Code Playgroud)
通过发布上面的语句,我希望看到accounting_line字段,每个accounting_line的预算金额之和和每个accounting_line的实际金额之和。
我到处搜索,找不到多个SUM函数的实例。非常感谢您的任何建议。
乔希
表数据如下:
Table: AccountingLine
act_line_id department
----------------------------------
1 Sales
2 HumanResources
Table: Budget
budget_id actg_line_id amount
----------------------------------------------
1 1 3500.00
2 2 5000.00
3 2 15000.00
Table: Actual
actual_id actg_line_id amount
----------------------------------------------
1 1 1000.00
2 2 500.00
3 2 9000.00
Run Code Online (Sandbox Code Playgroud)