OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

NodeJs fetch data by api-query-params

  • Thread starter Thread starter FootyFocusHD
  • Start date Start date
F

FootyFocusHD

Guest
github: https://github.com/khiemtongthong/projectSDN.git

Do the REST API (GET) endpoints /book?page=1&limit=10&populate=comments&fields=comments.comment&commentSearch=excellent populate to match all comment text that contain the word "excellent" or "good".

this is my solution but not return data in Postman :

`getBook: async (queryString) => { const page = parseInt(queryString.page) || 1; // Ensure page is a number const limit = parseInt(queryString.limit) || 10; // Default limit

Code:
const { filter, population, projection } = aqp(queryString);

delete filter.page;
delete filter.limit; // Remove limit from filter

if (queryString.minPrice) {
  filter.price = { $lt: parseFloat(queryString.minPrice) };
}

if (queryString.maxPrice) {
  filter.price = { $gt: parseFloat(queryString.maxPrice) };
}

// Comment search logic
if (queryString.commentSearch) {
  const searchTerms = queryString.commentSearch.split(',');
  filter.comments = {
    $elemMatch: {
      comment: { $regex: searchTerms.join('|'), $options: 'i' } 
    }
  };
}

let offset = (page - 1) * limit;
const result = await Book.find(filter)
  .populate(population)
  .skip(offset)
  .limit(limit)
  .select(projection)
  .exec();
return result;

},`

No data returned

<p>github: <a href="https://github.com/khiemtongthong/projectSDN.git" rel="nofollow noreferrer">https://github.com/khiemtongthong/projectSDN.git</a></p>
<p>Do the REST API (GET) endpoints
<code>/book?page=1&limit=10&populate=comments&fields=comments.comment&commentSearch=excellent</code> populate to match all comment text that contain the word "excellent" or "good".</p>
<p>this is my solution but not return data in Postman :</p>
<p>`getBook: async (queryString) => {
const page = parseInt(queryString.page) || 1; // Ensure page is a number
const limit = parseInt(queryString.limit) || 10; // Default limit</p>
<pre><code>const { filter, population, projection } = aqp(queryString);

delete filter.page;
delete filter.limit; // Remove limit from filter

if (queryString.minPrice) {
filter.price = { $lt: parseFloat(queryString.minPrice) };
}

if (queryString.maxPrice) {
filter.price = { $gt: parseFloat(queryString.maxPrice) };
}

// Comment search logic
if (queryString.commentSearch) {
const searchTerms = queryString.commentSearch.split(',');
filter.comments = {
$elemMatch: {
comment: { $regex: searchTerms.join('|'), $options: 'i' }
}
};
}

let offset = (page - 1) * limit;
const result = await Book.find(filter)
.populate(population)
.skip(offset)
.limit(limit)
.select(projection)
.exec();
return result;
</code></pre>
<p>},`</p>
<p><a href="https://i.sstatic.net/xt5HAciI.png" rel="nofollow noreferrer">No data returned</a></p>
 
Top