下一个 getStaticProps() 和运行 npm run build 时的猫鼬问题

时间:2021-04-01 03:45:28

标签: mongodb mongoose next.js

也许这是一个比看起来更简单的问题,我无法使用 Next 在静态生成的页面上使用组件。

但由于某种原因,我的应用程序始终无法预渲染这些页面。它在开发服务器上运行良好。所以这让我假设它与从数据库中提取的数据有关。但是当我直接从源代码检查数据时,数组很好,没有来自 Mongoose 的额外项目或包装元素。

我将 Next.js 与 MongoDB/Mongoose 一起使用。示例代码见下文。

import Layout from "../../Components/Layout/Layout";
import dbConnect from "../../utils/db-connect";
import Blog from "../../Models/blogpost";
import styles from "./singlePage.module.css";
import PostBody from "../../Components/Blog/PostBody/PostBody";
import HeaderDisplay from "../../Components/Blog/Header/HeaderDisplay/HeaderDisplay";

const Display = (props) => {
  const { posts } = props;

  return (
    <Layout>
      <div className={styles.singlePost}>
        <HeaderDisplay data={posts[0]} />
        <PostBody data={posts[0]} />
      </div>
    </Layout>
  );
};

export async function getStaticProps(context) {
  const { params } = context;
  const id = params.pid;
  await dbConnect();
  const posts = await Blog.find({});
  //stupid fix but it seems to be the one
  //const finalPosts = JSON.parse(JSON.stringify(posts));

  const finalPosts = posts.map((item) => {
    let finalPost = item.toObject();
    finalPost._id = finalPost._id.toString();
    finalPost.author.date = finalPost.author.date.toString();
    return finalPost;
  });
  const dataFinal = finalPosts.filter((item, index) => {
    return item._id === id;
  });
  console.log(finalPosts);
  console.log(dataFinal);

  if (dataFinal.length === 0) {
    return {
      notFound: true,
    };
  }

  return {
    props: {
      posts: dataFinal,
    },
    notFound: false,
  };
}

export async function getStaticPaths() {
  await dbConnect();
  const posts = await Blog.find({});
  //odd fix but it seems to be the one
  //const finalPosts = JSON.parse(JSON.stringify(posts));

  const finalPosts = posts.map((item) => {
    let finalPost = item.toObject();
    finalPost._id = finalPost._id.toString();
    finalPost.author.date = finalPost.author.date.toString();
    return finalPost;
  });
  const dynamicPaths = finalPosts.map((item, index) => {
    return { params: { pid: item._id } };
  });
  console.log(dynamicPaths);
  return {
    paths: dynamicPaths,
    fallback: true,
  };
}

下面是当我 console.log 传递到页面的数据时返回的内容。这是准确的。

//!Array we get from DB
/*
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
*/
//!After matching to an ID
/*
[
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
*/
//!pid pathways
/*
[
  { params: { pid: '6063cbde8172000667834400' } },
  { params: { pid: '6063cc358172000667834401' } },
  { params: { pid: '6063cc648172000667834402' } },
  { params: { pid: '6063e9265d575e08a28d002d' } },
  { params: { pid: '6063ea0c5d575e08a28d002e' } }
]

但是当我构建它时,我首先收到此错误 - 我假设这与在数据库中查询的数据有关。但同样,当我查看正在查询的数据时,_id 和 pid 匹配。

[ ===] info  - Generating static pages (6/14)
Error occurred prerendering page "/[pid]". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property '0' of undefined

整个构建日志

next build

info  - Creating an optimized production build  
info  - Compiled successfully
info  - Collecting page data .open and connected
info  - Collecting page data ..[
  { params: { pid: '6063cbde8172000667834400' } },
  { params: { pid: '6063cc358172000667834401' } },
  { params: { pid: '6063cc648172000667834402' } },
  { params: { pid: '6063e9265d575e08a28d002d' } },
  { params: { pid: '6063ea0c5d575e08a28d002e' } }
]
info  - Collecting page data  
[  ==] info  - Generating static pages (0/14)open and connected
open and connected
open and connected
[   =] info  - Generating static pages (0/14)open and connected
[
  {
    author: { date: 2021-03-31T01:09:45.948Z, name: 'Jon', read: 5 },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: 6063cbde8172000667834400,
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: { date: 2021-03-31T01:11:14.886Z, name: 'Mikey', read: 5 },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: 6063cc358172000667834401,
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: { date: 2021-03-31T01:11:52.375Z, name: 'Jon', read: 5 },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: 6063cc648172000667834402,
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: { date: 2021-03-31T03:14:40.677Z, name: 'Mikey', read: 5 },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: 6063e9265d575e08a28d002d,
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: { date: 2021-03-31T03:18:33.641Z, name: 'Mikey', read: 5 },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: 6063ea0c5d575e08a28d002e,
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[    ] info  - Generating static pages (3/14)1
[  ==] info  - Generating static pages (6/14)
Error occurred prerendering page "/[pid]". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property '0' of undefined
    at Display (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/.next/server/pages/[pid].js:589:20)
    at d (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:33:498)
    at bb (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:16)
    at a.b.render (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:42:43)
    at a.b.read (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:41:83)
    at exports.renderToString (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:52:138)
    at Object.renderPage (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/next-server/server/render.js:54:851)
    at Function.getInitialProps (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/.next/server/pages/_document.js:719:19)
    at loadGetInitialProps (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/next-server/lib/utils.js:5:101)
    at renderToHTML (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/next-server/server/render.js:54:1142)
    at async /Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/export/worker.js:26:6
    at async Span.traceAsyncFn (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/telemetry/trace/trace.js:5:584)
open and connected
[ ===] info  - Generating static pages (6/14)open and connected
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  }
]
[====] info  - Generating static pages (10/14)[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 18:09:45 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'React', 'Mongo', 'Next' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cbde8172000667834400',
    title: 'Tester 1',
    quip: 'Tester 1',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:14 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc358172000667834401',
    title: 'TESTER 2',
    quip: 'Tester 2',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 18:11:52 GMT-0700 (Pacific Daylight Time)',
      name: 'Jon',
      read: 5
    },
    tags: [ 'Next', 'Mongo' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063cc648172000667834402',
    title: 'TESTER 3',
    quip: 'tester 3',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:14:40 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'Express', 'JS', 'Node' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063e9265d575e08a28d002d',
    title: 'TESTER 4',
    quip: 'Teester 4',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617148584/Blog%20Images/sort_ticpka.jpg',
    __v: 0
  },
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
[
  {
    author: {
      date: 'Tue Mar 30 2021 20:18:33 GMT-0700 (Pacific Daylight Time)',
      name: 'Mikey',
      read: 5
    },
    tags: [ 'Mongo', 'JS' ],
    body: [ [Object], [Object], [Object] ],
    _id: '6063ea0c5d575e08a28d002e',
    title: 'TESTER 5',
    quip: 'tester 5',
    mainImg: 'https://res.cloudinary.com/dxtqihvgt/image/upload/v1617145104/Blog%20Images/clickity_uj6qud.jpg',
    __v: 0
  }
]
info  - Generating static pages (14/14)

> Build error occurred
Error: Export encountered errors on following paths:
    /[pid]
    at /Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/export/index.js:31:1103
    at async Span.traceAsyncFn (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/telemetry/trace/trace.js:5:584)
    at async /Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/build/index.js:41:49
    at async Span.traceAsyncFn (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/telemetry/trace/trace.js:5:584)
    at async /Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/build/index.js:23:1472
    at async Span.traceAsyncFn (/Users/jonathankirkpatrick/Desktop/Hired/evronBlog/evronblog/node_modules/next/dist/telemetry/trace/trace.js:5:584)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! evronblog@0.1.0 build: `next build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the evronblog@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jonathankirkpatrick/.npm/_logs/2021-04-01T03_34_45_531Z-debug.log

可能是什么原因造成的?它基本上是在告诉我有一个被拉动的对象没有我相信的属性?

为了更好地衡量,我调用的函数根据 Vercel 和 Next 团队提供的示例连接到数据库。 Next mongoose examples

import mongoose from "mongoose";

async function dbConnect() {
  // check if we have a connection to the database or if it's currently
  // connecting or disconnecting (readyState 1, 2 and 3)
  if (mongoose.connection.readyState >= 1) {
    return;
  }

  return mongoose
    .connect("mongodb://localhost:27017/blog-almost", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
      //maxIdleTimeMS: 1000,
      //serverSelectionTimeoutMS: 1000,
      //socketTimeoutMS: 2000,
    })
    .then(console.log("open and connected"));
}

export default dbConnect;

1 个答案:

答案 0 :(得分:0)

所以这似乎成功了,我将回退设置为 false

export async function getStaticPaths() {
  await dbConnect();
  const posts = await Blog.find({});
  //odd fix but it seems to be the one
  //const finalPosts = JSON.parse(JSON.stringify(posts));

  const finalPosts = posts.map((item) => {
    let finalPost = item.toObject();
    finalPost._id = finalPost._id.toString();
    finalPost.author.date = finalPost.author.date.toString();
    return finalPost;
  });
  const dynamicPaths = finalPosts.map((item, index) => {
    return { params: { pid: item._id } };
  });
  console.log(dynamicPaths);
  return {
    paths: dynamicPaths,
    //fallback: true
    fallback: false,
  };
}

我还在“显示”页面上散布了传递到我的组件中的数据。不确定这是否有必要,但绝对更容易遵循。

const Display = (props) => {
  const posts = { ...props.post };
  const author = { ...posts.author };
  const tags = [...posts.tags];
  const title = posts.title;
  const quip = posts.quip;
  const mainImg = posts.mainImg;
  const body = posts.body;

  let display = (
    <Layout>
      <div className={styles.singlePost}>
        <HeaderDisplay
          author={author}
          title={title}
          tags={tags}
          mainImg={mainImg}
        />
        <PostBody body={body} quip={quip} />
      </div>
    </Layout>
  );

  return <Fragment>{display}</Fragment>;
};

以及我非常想知道但从未看过的信息

---后备:“阻塞”

Next.js fallback: "blocking"