admin管理员组文章数量:1326123
Is there a function similar to q=sort&
or q=created:&
to limit number of results from a JavaScript fetch?
fetch('')
.then((res) => res.json())
.then((data) => { }
Is there a function similar to q=sort&
or q=created:&
to limit number of results from a JavaScript fetch?
fetch('https://jsonplaceholder.typicode./posts')
.then((res) => res.json())
.then((data) => { }
Share
Improve this question
edited May 22, 2019 at 20:28
Grant Noe
1,0039 silver badges29 bronze badges
asked Sep 28, 2018 at 14:33
medveddimitrimedveddimitri
631 gold badge1 silver badge3 bronze badges
2
-
What about
.filter
? – Arfeo Commented Sep 28, 2018 at 14:36 - This didn't deserve a downvote, good question and good description to it, I was looking for the same thing – logos_164 Commented Dec 16, 2019 at 19:12
3 Answers
Reset to default 4The best solution, of course, is if the https://jsonplaceholder.typicode./posts
endpoint documents a limit or filter parameter you can send it.
Assuming the result is an array, or contains an array, the very-much-second-best solution is to filter
the result (to apply criteria) and/or slice
the result (to just apply a limit):
fetch('https://jsonplaceholder.typicode./posts')
.then((res) => res.json())
.then((data) => {
data = data.filter(entry => entry.created > someValue) // Created after X
.slice(0, 1000); // Limit to 1000
// ...use data...
})
.catch(error => { // <=== Don't forget to handle errors
// Handle error...
});
Note: Your fetch
call is missing a check on res.ok
(it's not just you, a lot of people make that mistake so many that I wrote it up on my anemic little blog):
fetch('https://jsonplaceholder.typicode./posts')
.then((res) => { // ***
if (!res.ok) { // ***
throw new Error("HTTP error " + res.status); // ***
} // ***
}) // ***
.then((res) => res.json())
.then((data) => {
data = data.filter(entry => entry.created > someValue)
.slice(0, 1000);
// ...use data...
})
.catch(error => {
// Handle error...
});
fetch(`http://localhost:3000/services?_page=${page}&_limit=3`)
You can add a payload to the body of the fetch, see above.
From https://developer.mozilla/en-US/docs/Web/API/Fetch_API/Using_Fetch:
postData(`http://example./answer`, {answer: 42})
.then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
.catch(error => console.error(error));
function postData(url = ``, data = {}) {
// Default options are marked with *
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
}
Not sure what exactly you want so here are 3 possibilities:
You can add a payload to the body of the fetch, see above.
You could simply url-encode it.
On res.json()) .then((data) => { } ... You can filter the data you want.
Hope this helps.
本文标签: Limit fetch results from javascript fetchStack Overflow
版权声明:本文标题:Limit fetch results from javascript fetch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191602a2430318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论