-
Currently I am trying to do this, this way I can grab the contents so that I can render it or advertise it in my website. // server/api/contents.ts
export default defineEventHandler(async (event) => {
const blogs = await queryContent("blog")
.where({ active: 1 })
.sort({ _file: -1 })
.limit(10)
.find();
return blogs;
}); But shows this error: [nuxt] [request error] [unhandled] [500] queryContent is not defined
at /D:/Projects/_PersonalProjects/BroJenuelBlog/.nuxt/dev/index.mjs:3342:17
at /D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:527:47
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:592:19
at async Server.nodeHandler (/D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:538:7)
[nuxt] [request error] [unhandled] [500] queryContent is not defined
at /D:/Projects/_PersonalProjects/BroJenuelBlog/.nuxt/dev/index.mjs:3342:17
at /D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:525:14
at Object.handler (/D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:684:12)
at /D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:592:31
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Server.nodeHandler (/D:/Projects/_PersonalProjects/BroJenuelBlog/node_modules/h3/dist/index.mjs:538:7) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I have not seen any way (for now) to use When you're not using regex (or put the regex in quotes e.g. const query = {
where: {
$regex: {
_path: '/^\/blog/' // path of a file starts with `/blog`; the regex is contained in quotes
},
active: 1
},
sort: {
_file: -1
},
limit: 10
}
const blogs = await $fetch(`/api/_content/query?_params=${JSON.stringify(query)}`)
or alternatively the whole stringified object: const blogs = await $fetch('/api/_content/query?_params={"where":{"_path":{"$regex":"/^\/blog/"},"active":1},"sort":{"_file":-1},"limit":10}') for context of the |
Beta Was this translation helpful? Give feedback.
-
For those who want to get their articles, in API routes you can use the // server/api/recent-contents/ts
import { serverQueryContent } from "#content/server";
export default defineEventHandler(async (event) => {
// Fetch all documents
const docs = await serverQueryContent(event).limit(10).find();
const baseUrl = "https://bro.brojenuel.com";
return docs.map((doc) => {
return {
path: `${baseUrl}${doc._path}`,
title: doc.title,
description: doc.description,
keywords: doc.keywords,
date: doc.date,
image: doc.image,
image_thumbnail: doc.image_thumbnail,
image_title: doc.image_title,
};
});
});
and can be fetched through: https://bro.brojenuel.com/api/recent-contents |
Beta Was this translation helpful? Give feedback.
For those who want to get their articles, in API routes you can use the
serverQueryContent
API function provided by the#content/server
This is a sample, I used on my blog that way I can use it to show to my portfolio website.