检查 REST 端点返回的 JSON 中的值
Check for a value in JSON returned in a REST endpoint
我对 React-native(和 javascript)有点陌生,我正在努力做到这一点。
我有一个 api 有一个用户
https://api.example.com/users/
我希望它在我到达终点时检查 api returns 是否是一个值
https://api.example.com/users/{用户名}
所以像这样:
async function getUsersFromApi(userName) {
try {
let response = await
fetch('https://api.example.com/users/'+userName);
let responseJson = await response.json();
//if endpoint returns response
// alert("endpoint exists")
} catch(error) {
console.error(error);
}
}
我在评论中写的部分是我正在努力的部分。任何帮助将不胜感激。
response.ok property will tell you whether the request was successful and this is the usual way to determine whether a resource exists or not. If a user with the given username
does not exist, the REST API should return a 404 error,这将导致 response.ok
变为 false
。
const response = await fetch('https://api.example.com/users/'+userName);
const userExists = response.ok;
请注意,如果您根本不打算使用响应主体,则执行 HEAD 请求更为合适。这指示服务器它不应该 return 响应主体并跳过任何可能会被浪费的相关计算。因此请求在时间和带宽上都更有效率。
const response = await fetch('https://api.example.com/users/'+userName. {
method : 'GET'
});
const userExists = response.ok;
编辑:回答下面的原始问题
假设您有一个 users
数组,您应该使用 Array#some() 遍历它并确定其中的一个对象是否具有与给定的 username
相匹配的用户名字段到你的功能。
const userExists = users.some((user) => {
return user.username === username;
});
console.log(userExists); // true or false
另一方面,如果您需要 return 该用户对象而不是布尔值,则应改用 Array#find()。
const user = users.find((user) => {
return user.username === username;
});
console.log(user); // user object or undefined
在find()
的情况下,一定要处理找不到用户且return未定义的情况。如果您不小心,您最终可能会在 undefined
上访问 属性,这将引发错误。
我对 React-native(和 javascript)有点陌生,我正在努力做到这一点。
我有一个 api 有一个用户
https://api.example.com/users/
我希望它在我到达终点时检查 api returns 是否是一个值
https://api.example.com/users/{用户名}
所以像这样:
async function getUsersFromApi(userName) {
try {
let response = await
fetch('https://api.example.com/users/'+userName);
let responseJson = await response.json();
//if endpoint returns response
// alert("endpoint exists")
} catch(error) {
console.error(error);
}
}
我在评论中写的部分是我正在努力的部分。任何帮助将不胜感激。
response.ok property will tell you whether the request was successful and this is the usual way to determine whether a resource exists or not. If a user with the given username
does not exist, the REST API should return a 404 error,这将导致 response.ok
变为 false
。
const response = await fetch('https://api.example.com/users/'+userName);
const userExists = response.ok;
请注意,如果您根本不打算使用响应主体,则执行 HEAD 请求更为合适。这指示服务器它不应该 return 响应主体并跳过任何可能会被浪费的相关计算。因此请求在时间和带宽上都更有效率。
const response = await fetch('https://api.example.com/users/'+userName. {
method : 'GET'
});
const userExists = response.ok;
编辑:回答下面的原始问题
假设您有一个 users
数组,您应该使用 Array#some() 遍历它并确定其中的一个对象是否具有与给定的 username
相匹配的用户名字段到你的功能。
const userExists = users.some((user) => {
return user.username === username;
});
console.log(userExists); // true or false
另一方面,如果您需要 return 该用户对象而不是布尔值,则应改用 Array#find()。
const user = users.find((user) => {
return user.username === username;
});
console.log(user); // user object or undefined
在find()
的情况下,一定要处理找不到用户且return未定义的情况。如果您不小心,您最终可能会在 undefined
上访问 属性,这将引发错误。