首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >blogRoutes : Nuxt: Nuxt不可迭代

blogRoutes : Nuxt: Nuxt不可迭代
EN

Stack Overflow用户
提问于 2019-09-20 05:27:35
回答 1查看 233关注 0票数 0

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

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

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-23 10:25:46

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

还要注意,您似乎没有返回正确的data,您希望返回一个字符串数组,而不是一个字符串数组的and数组。

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58019130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档