Nuxt: TypeError: blogRoutes is not iterable

Nuxt: TypeError: blogRoutes is not iterable

我正在测试 Nuxt,特别是如何为静态站点模式生成路由。当我部署到 Netlify 时,我在部署日志中收到错误 TypeError: blogRoutes is not iterable。这是什么意思,因为这两个请求的输出似乎都是简单字符串的数组。任何帮助将不胜感激。

下面是一个稍微做作的示例,因为我对 Contentful 进行了两次调用,实际上这是对 Shopify 和 Contentful 的调用。

generate: {
    routes: () => {
        const client = contentful.createClient(config)

        const blogRoutes = client
            .getEntries({
                content_type: 'blogPost'
            })
            .then(response => {
                return response.items.map(
                    entry => `/blog/${entry.fields.slug}`
                )
            })
        const collectionRoutes = client
            .getEntries({
                content_type: 'collection'
            })
            .then(response => {
                return response.items.map(
                    entry => `/collections/${entry.fields.slug}`
                )
            })
        const routes = [[...blogRoutes, ...collectionRoutes]]

        return routes
    }
}

blogRoutescollectionRoutes 都是 promises,不是数组。它们解析为数组(我假设),但您需要等待它们,或使用 Promise.all().

另请注意,您似乎没有 return 正确 data,您想要 return 一个字符串数组,而不是字符串数组的数组.

generate: {
    routes: async () => { // add async here
        const client = contentful.createClient(config)

        const blogRoutes = await client // add await here
            .getEntries({
                content_type: 'blogPost'
            })
            .then(response => {
                return response.items.map(
                    entry => `/blog/${entry.fields.slug}`
                )
            })
        const collectionRoutes = await client // add await here
            .getEntries({
                content_type: 'collection'
            })
            .then(response => {
                return response.items.map(
                    entry => `/collections/${entry.fields.slug}`
                )
            })

        const routes = [...blogRoutes, ...collectionRoutes] // return a single array of strings

        return routes
    }
}