盖茨比无法为所有数据库项目创建静态页面

时间:2020-06-15 13:56:13

标签: graphql gatsby

我一直在尝试Gatsby和MongoDB教程:https://developer.mongodb.com/how-to/gatsby-modern-blog

一切正常,直到添加文件gatsby-node.js。理想情况下,添加此文件后,它应该从数据库中生成400本书的页面,但不是。

代码没有任何错误。我想念什么吗?

任何帮助将不胜感激。

book.js(用于创建页面的模板)

import React from "react"
import { graphql } from "gatsby"
import Layout from "./layout"

class Item extends React.Component {
  render() {
    const book = this.props.data.mongodbGatsbyBooks

    return (
      <Layout>
        <div>
          <img src={book.thumbnailUrl} alt={book.title} />
          <h1>{book.title}</h1>
        </div>
      </Layout>
    )
  }
}

export default Item

export const pageQuery = graphql`
  query($id: String!) {
    mongodbGatsbyBooks(id: { eq: $id }) {
      id
      title
      longDescription
      thumbnailUrl
      isbn
      pageCount
      publishedDate(formatString: "MMMM DD, YYYY")
      authors
      categories
    }
  }

gatsby-node.js

const path = require('path')

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions

  const { data } = await graphql(`
    {
      books: allMongodbGatsbyBooks {
        edges {
          node {
            id
          }
        }
      }
    }
  `)

  const pageTemplate = path.resolve('./src/components/book.js')

  for (const { node } of data.books.edges) {
    createPage({
      path: '/book/${node.id}/',
      component: pageTemplate,
      context: {
        id: node.id,
      },
    })
  }
}

0 个答案:

没有答案