Вы можете использовать эту пользовательскую библиотеку (написанную с помощью Promise) для выполнения удаленного вызова.
function $http(apiConfig) {
return new Promise(function (resolve, reject) {
var client = new XMLHttpRequest();
client.open(apiConfig.method, apiConfig.url);
client.send();
client.onload = function () {
if (this.status >= 200 && this.status < 300) {
// Performs the function "resolve" when this.status is equal to 2xx.
// Your logic here.
resolve(this.response);
}
else {
// Performs the function "reject" when this.status is different than 2xx.
reject(this.statusText);
}
};
client.onerror = function () {
reject(this.statusText);
};
});
}
Пример простого использования:
$http({
method: 'get',
url: 'google.com'
}).then(function(response) {
console.log(response);
}, function(error) {
console.log(error)
});
Оценка condition
привела к NA
. Условие if
должно иметь либо результат TRUE
, либо FALSE
.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
Это может случиться случайно в результате результатов вычислений:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
Чтобы проверить, отсутствует ли объект, используйте is.na(x)
, а не x == NA
.
См. также связанные ошибки:
Ошибка в if / while (условие) {: аргумент имеет нулевую длину
Ошибка в if / while (условие): аргумент не интерпретируется как логический
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
Я столкнулся с этим при проверке нулевой или пустой строки
if (x == NULL || x == '') {
изменил ее на
if (is.null(x) || x == '') {