Need help with MERN Project

Hi,

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.

Thank you in advance.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

I need help with implementing CODE 1 with CODE 2.

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);
});