2017-06-01 67 views
1

我寫有反應,節點的應用程序,但我的本地主機未運行,而我有這樣的錯誤:無法讀取的不確定(節點JS)屬性「路徑」

UNHANDLED REJECTION TypeError: Cannot read property 'path' of undefined 

所有NPM-模塊安裝,但這個錯誤不允許啓動我的項目。這是我第一次嘗試用節點編寫代碼,所以也許答案就近了。 下面我的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

它並不是完全顯示行數,但它顯示了這一點: D:\ IT \ Valeria \ Work \ Project \ client-only-paths \ node_modules \ bluebird \ js \ release \ async.js:61 'fn = function(){throw arg; };' –

回答

0

我覺得你的問題是在這裏:

).then(result => { 
    if (result.errors) { 
     console.log(result.errors) 
     reject(result.errors) 
    } 

你是不是在if塊早回來,所以功能將繼續,並試圖後做的一切。

相反,只是扔在那裏一回:

return reject(result.errors) // Now will not continue

但更具體的,對你的承諾,這就是爲什麼你看到UNHANDLED REJECTION警告沒有錯誤處理程序。

+0

不幸的是,它並沒有幫助,但我解決了它。 我刪除npm模塊,重新啓動我的電腦並重新安裝:) –

+0

對不起:祝你好運 – sparrow

相關問題