I have a problem with ReactJS and NodeJS related to filtering and paginating data. The problem is a bit more complex than just posting it on the forum here since I have a bit explaining to do what I want to achieve and what is not working.
Is it possible that someone can help me via some chat if this forum has one, or on facebook/whatsapp/email/discordapp if that is allowed. If not, is there someone here that will be willing to really follow along and help me since in general on other forums and chats I tried to get help, people give up on 70% and then I don’t get nothing, just time wasted for trying to explain what is happening.
The first code is working and it basically gets all profiles or if you filter data with some params then it send profiles that are filtered.
The code 2 below is a working code from react-pagination documentation and it does the pagination or the slicing part so i need to somehow implement it in my code 1 but i get lost every time.
CODE 1
// @route GET api/profile/all
// @description Get all profile
// @access Public
router.get("/all", (req, res) => {
const errors = {};
console.log("query is giving me:", req.query);
//console.log("res je:", res);
//Object of all query parameters
const queries = req.query;
//Save the query into a constant variable, so we can filter through it
const search = Profile.find().populate("user", ["name", "picture"]);
//console.log("search", search);
//Loop through all keys of the object
Object.keys(queries).forEach(key => {
// Apply a filter to the search for each key-value pair of the query Object
// This alters the search variable
console.log("key", key);
search.where(key, queries[key]);
});
//Return the final result, or a error
search
.then(profiles => {
if (!profiles) {
errors.noprofile = "There are no profiles for this user";
return res.status(404).json(errors);
}
res.json(profiles);
})
.catch(err =>
res.status(404).json({ profile: "There are no profiles for this user" })
);
});
CODE2:
app.get('/comments', function(req, res) {
var items = JSON.parse(fs.readFileSync(DATA));
var offset = req.query.offset ? parseInt(req.query.offset, 10) : 0;
var nextOffset = offset + PER_PAGE;
var previousOffset = offset - PER_PAGE < 1 ? 0 : offset - PER_PAGE;
var meta = {
limit: PER_PAGE,
next: util.format('?limit=%s&offset=%s', PER_PAGE, nextOffset),
offset: req.query.offset,
previous: util.format('?limit=%s&offset=%s', PER_PAGE, previousOffset),
total_count: items.length,
};
var json = {
meta: meta,
comments: getPaginatedItems(items, offset),
};
return res.json(json);
});