我用react和node编写app,但是我的localhost没有运行,我有这个错误:
UNHANDLED REJECTION TypeError: Cannot read property 'path' of undefined
已安装所有npm-modules,但此错误不允许启动我的项目。这是我第一次尝试用节点编写代码,所以也许答案就在附近。 我的node.js如下:
const _ = require("lodash")
const Promise = require("bluebird")
const path = require("path")
const select = require(`unist-util-select`)
const fs = require(`fs-extra`)
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
return new Promise((resolve, reject) => {
const pages = []
const blogPost = path.resolve("./src/templates/blog-post.js")
resolve(
graphql(
`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Create blog posts pages.
_.each(result.data.allMarkdownRemark.edges, edge => {
upsertPage({
path: edge.node.fields.slug, // required
component: blogPost,
context: {
slug: edge.node.fields.slug,
},
})
})
})
)
})
}
// Add custom slug for blog posts to both File and MarkdownRemark nodes.
exports.onNodeCreate = ({ node, boundActionCreators, getNode }) => {
const { addFieldToNode } = boundActionCreators
if (node.internal.type === `File`) {
const parsedFilePath = path.parse(node.relativePath)
const slug = `/${parsedFilePath.dir}/`
addFieldToNode({ node, fieldName: `slug`, fieldValue: slug })
} else if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent)
addFieldToNode({
node,
fieldName: `slug`,
fieldValue: fileNode.fields.slug,
})
}
}
答案 0 :(得分:0)
我认为你的问题在这里:
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
你没有在if块的早期返回,所以该功能正在继续,并试图在之后做所有事情。
相反,只需在那里抛出一个回报:
return reject(result.errors) // Now will not continue
但更具体地说,您的承诺上没有错误处理程序,这就是您看到UNHANDLED REJECTION
警告的原因。