当前我有上面的工作代码,但是不建议使用全局变量的问题,因为这是文件而不是类 我想在函数内添加变量并将其作为对象返回并在其他函数中使用它们,但是这样做之后我收到错误消息 ``无法读取未定义的属性'sms_procesados'。''
我做错了什么?
原始工作代码
export const sendSMS = (data) => {
let smsResponse = send(data);
return checkResponse(data, smsResponse);
}
var results = [];
var numbers = [];
const send = (data) => {
console.log('Sending SMS to ' + data.numbers.length + " recipients with message [" + data.message + "]");
try {
while (data.numbers.length > 0) {
let toSend = data.numbers.splice(0, 1000);
const response = HTTP.call('POST', Meteor.settings.hablame.endpoint, {
params: {
api: Meteor.settings.hablame.apiKey,
cliente: Meteor.settings.hablame.user,
numero: toSend.join(),
sms: data.message
}
});
numbers.push(toSend);
results.push(response);
}
return results;
} catch (e) {
console.log(e);
return false;
}
}
const checkResponse = (data, response) => {
var output = [];
for (i = 0; i < results.length; i++) {
const sent = response[i].data.sms_procesados;
const intended = numbers[i].length;
if (response[i].data.resultado !== 0) {
console.log(response[i].data.resultado !== 0 + " <- shit 2");
output.push(false);
} else if (sent == intended) {
console.log("All SMS messages where successfully sent");
output.push(true);
} else {
console.warn(`Some SMS messages failed [${ (intended - sent)} out of ${intended}]`);
output.push(true);
}
}
//check the array of outputs if a chunk of numbers wasnt sent
for(i = 0; i< output.length; i++){
if(output[i] == false){
return false;
}
}
return true;
}
带有错误的重构代码:
export const sendSMS = (data) => {
let send_return = send(data);
let smsResponse = send_return.results;
let smsNumbers = send_return.numbers;
return checkResponse(data, smsResponse,smsNumbers);
}
const send = (data) => {
let results = [];
let numbers = [];
let send_numbers = data.numbers.slice();
console.log('Sending SMS to ' + data.numbers.length + " recipients with message [" + data.message + "]");
try {
while (send_numbers.length > 0) {
let toSend = send_numbers.splice(0, 1000);
const response = HTTP.call('POST', Meteor.settings.hablame.endpoint, {
params: {
api: Meteor.settings.hablame.apiKey,
cliente: Meteor.settings.hablame.user,
numero: toSend.join(),
sms: data.message
}
});
numbers.push(toSend);
results.push(response);
}
return {results : results, numbers : numbers};
} catch (e) {
console.log(e);
return false;
}
}
const checkResponse = (data, response, numbers) => {
var output = [];
for (i = 0; i < response.length; i++) {
const sent = response[i].data.sms_procesados;
const intended = numbers[i].length;
if (response[i].data.resultado !== 0) {
console.log(response[i].data.resultado !== 0 + " <- shit 2");
output.push(false);
} else if (sent == intended) {
console.log("All SMS messages where successfully sent");
output.push(true);
} else {
console.warn(`Some SMS messages failed [${ (intended - sent)} out of ${intended}]`);
output.push(true);
}
}
//check the array of outputs if a chunk of numbers wasnt sent
for(i = 0; i< output.length; i++){
if(output[i] == false){
return false;
}
}
return true;
}