Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] Can queryCollectionWithEvent be used as a replacement for serverQueryContent? How should it be imported? #2968

Closed
aatrooox opened this issue Jan 14, 2025 · 5 comments
Labels
enhancement New feature or request

Comments

@aatrooox
Copy link

In Content v2, serverQueryContent could be used to fetch content.

import { serverQueryContent } from '#content/server'

What should be used in v3?

I found queryCollectionWithEvent in the source code.

Is it safe to use this API now? How should it be imported?

@aatrooox aatrooox added the enhancement New feature or request label Jan 14, 2025
@aatrooox aatrooox changed the title Can queryCollectionWithEvent be used as a replacement for serverQueryContent? How should it be imported? [v3]Can queryCollectionWithEvent be used as a replacement for serverQueryContent? How should it be imported? Jan 14, 2025
@aatrooox aatrooox changed the title [v3]Can queryCollectionWithEvent be used as a replacement for serverQueryContent? How should it be imported? [v3] Can queryCollectionWithEvent be used as a replacement for serverQueryContent? How should it be imported? Jan 14, 2025
Copy link
Collaborator

larbish commented Jan 14, 2025

You can simply use queryCollection with the event as first param:

export default defineEventHandler(async (event) => {
  const items = await queryCollection(event, 'collectionName').all()

  if (!items) {
    throw createError({
      statusCode: 404,
      statusMessage: 'Items not found'
    })
  }

  return items
})

@farnabaz We're lacking types in this case, wrong method definition is used.

@aatrooox
Copy link
Author

aatrooox commented Jan 14, 2025

it works now.

TypeScript's type error messages misled me.

I used @ts-ignore to temporarily suppress the error.

Looking forward to your next update! 😊

@farnabaz
Copy link
Member

This should be fixed in the latest commit release.
Note that you should have server/tsconfig.json file which extends .nuxt/tsconfig.server.json

{
  "extends": "../.nuxt/tsconfig.server.json"
}

@DSYZayn
Copy link

DSYZayn commented Jan 24, 2025

@farnabaz I've done these steps but got failure in dev

[14:29:31]  WARN  "#content/server" is imported by "node_modules/.pnpm/@nuxtjs+sitemap@5.3.5_h3@1.13.1_magicast@0.3.5_rollup@4.30.1_vite@6.0.7_@types+node@22.10.5_j_2oyst4ksuidprlez5dr6q7axeu/node_modules/@nuxtjs/sitemap/dist/runtime/nitro/routes/__sitemap__/nuxt-content-urls.js", but could not be resolved – treating it as an external dependency.        

✔ Nuxt Nitro server built in 3578 ms                                                                                                                             nitro 14:29:34
ℹ Vite client warmed up in 5ms                                                                                                                                         14:29:34   

 ERROR  [worker reload] [worker init] E:/code/nuxt-blog/.nuxt/dev/index.mjs failed                                                                                      14:29:34  



  [cause]: Package import specifier "#content/server" is not defined in package E:\code\nuxt-blog\package.json imported from E:\code\nuxt-blog\.nuxt\dev\index.mjs

      at importNotDefined (node:internal/modules/esm/resolve:286:10)
      at packageImportsResolve (node:internal/modules/esm/resolve:736:9)
      at moduleResolve (node:internal/modules/esm/resolve:905:16)
      at defaultResolve (node:internal/modules/esm/resolve:1124:11)
      at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
      at ModuleLoader.resolve (node:internal/modules/esm/loader:526:25)
      at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:249:38)
      at ModuleJob._link (node:internal/modules/esm/module_job:126:49)
// server/routes/rss.xml.ts
import { Feed } from 'feed'

const basePath = 'https://blog.xx.cn'

export default defineEventHandler(async (event) => {
  setHeader(event, 'content-type', 'text/xml')
  const docs = await queryCollection(event, 'blogs').order('date', 'DESC').all()

  const feed = new Feed({
    title: "Zayn's personal blog site",
    description: "Zayn's personal blog site",
    id: basePath,
    link: basePath,
    language: 'zh',
    favicon: `${basePath}/favicon.ico`,
    copyright: 'MIT',
    author: {
      name: 'xx',
      email: 'xx@gmail.com',
      link: basePath,
    },
  })

  // Add the feed items
  docs.forEach((doc) => {
    feed.addItem({
      title: doc.title || '',
      id: basePath + doc.path,
      link: basePath + doc.path,
      description: doc.description,
      content: doc.description,
      date: new Date(doc.date as string),
    })
  })

  return feed.rss2()
})
  • nuxt^3.15.1
  • @nuxt/content^3.0.0

Copy link
Member

@DSYZayn Looks like you are using older version of @nuxtjs/sitemap which is compatible with Content v2. You should use v7.2.0 or later to support Content v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants