gpt4 book ai didi

javascript - Node Controller - 异步传递匿名函数

转载 作者:行者123 更新时间:2023-12-02 14:25:00 24 4
gpt4 key购买 nike

尝试使用异步创建 Controller ,但我无法传递匿名函数作为第三个参数。我不断收到意外标记 { - '有什么想法吗?如果我直接在参数内传递函数(err,response),错误就会消失。我基本上试图迭代将返回的两个对象,找到每个对象的合约名称,然后分配为该合约分配的数据数组。

var request = require('request'),
helpers = require('../../helpers.js'),
async = require('async');

module.exports.getStatementBreakdown = function(req, res) {
var httpGet,
response,
urls = [
'/financial-advances/',
'/financial-adjustments/'
];

httpGet = function(url, callback) {
var options = helpers.buildAPIRequestOptions(req, url);
request(options,
function(err, res, body) {
var data = {};
if(!err && res.statusCode === 200) {
data = JSON.parse(body);
}
callback(err, data);
}
);
};

response = function(err, responses) {}

async.map(urls, httpGet, response) {
var statementBreakdown = {},
response,
breakdown,
i,
j,
contractName,
key;

for(i = 0; i < responses.length; i++) {
response = responses[i];
for(key in response) {
if(key !== 'meta' || key !== 'notifications') {
breakdown = response[key];
for(j = 0; j < breakdown.length; j++) {
contractName = breakdown[j].reimbursementContract.name;
}
}
}
}
statementBreakdown[contractName] = [];
statementBreakdown[contractName].push(breakdown);
res.send(statementBreakdown);
});
};

最佳答案

根据您发布的代码示例,您会得到一个意外的标记,因为您的花括号位于错误的位置。

参见此处:async.map(urls, httpGet, response) {?那个花括号是意想不到的标记。

当存在不应出现的字符时,Javascript 会给出意外的标记。在本例中,您在函数调用之后添加了一个大括号。控制流语句和函数声明之后需要大括号。

我不确定你到底想要什么,但也许是这样的?

async.map(urls, httpGet, function(err, responses) {
var statementBreakdown = {},
response,
breakdown,
i,
j,
contractName,
key;

for(i = 0; i < responses.length; i++) {
response = responses[i];
for(key in response) {
if(key !== 'meta' || key !== 'notifications') {
breakdown = response[key];
for(j = 0; j < breakdown.length; j++) {
contractName = breakdown[j].reimbursementContract.name;
}
}
}
}
statementBreakdown[contractName] = [];
statementBreakdown[contractName].push(breakdown);
res.send(statementBreakdown);
});
});

有关 async.map 的更多帮助,请参阅 docs .

关于javascript - Node Controller - 异步传递匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38353872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com