首页 > 解决方案 > 为什么我会收到此错误 TypeError: Cannot read property 'utf8Slice' of undefined

问题描述

我对后端相关代码一无所知,所以我决定从最近开始逐步学习一些东西,所以我决定遵循一个教程,现在我遇到了一个我研究过但仍然无法修复的错误。

我有这个

const postsSchema = require('./posts')

const resolvers = [
    postsSchema.resolvers
]

const typeDefs = [
    postsSchema.schema
]

module.exports = {
    resolvers,
    typeDefs
}
const trendingPosts = require('./mocks/trending')
const featuredPosts = require('./mocks/featured')
const fs = require('fs')
const path = require('path')

module.exports = {
    resolvers: {
        Query: {
            trendingPosts: () => trendingPosts,
            featuredPosts: () => featuredPosts,
            recentPosts: () => [
                ...trendingPosts,
                ...featuredPosts,
                ...featuredPosts 
            ]
        }
    }, 
    schema: fs.readFileSync(
        path.resolve(
            __dirname,
            './posts-schema.graphql'
        )
    ).toString
    
}

这是我的 GraphQl

type Query {
    trendingPosts: [Post]!
    featuredPosts: [Post]!
    recentPosts: [Post]!
}

type Post {
    id: Int,
    title: String
    date: String
    categories: [String]
    author: String
    description: String
    image: String
}

我很确定我遵循了教程中的整个过程,但是当我运行节点应用程序时,我在终端中收到了这个错误

buffer.js:778
    return this.utf8Slice(0, this.length);
                ^

TypeError: Cannot read property 'utf8Slice' of undefined
    at toString (buffer.js:778:17)
    at C:\Users\Natey\Desktop\Code Related\my-app\graphql\node_modules\@graphql-tools\schema\index.cjs.js:195:65
    at Array.forEach (<anonymous>)
    at concatenateTypeDefs (C:\Users\Natey\Desktop\Code Related\my-app\graphql\node_modules\@graphql-tools\schema\index.cjs.js:191:24)
    at buildDocumentFromTypeDefinitions (C:\Users\Natey\Desktop\Code Related\my-app\graphql\node_modules\@graphql-tools\schema\index.cjs.js:231:46)
    at buildSchemaFromTypeDefinitions (C:\Users\Natey\Desktop\Code Related\my-app\graphql\node_modules\@graphql-tools\schema\index.cjs.js:213:22)
    at makeExecutableSchema (C:\Users\Natey\Desktop\Code Related\my-app\graphql\node_modules\@graphql-tools\schema\index.cjs.js:811:32)
    at Object.<anonymous> (C:\Users\Natey\Desktop\Code Related\my-app\graphql\app.js:8:13)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

我需要帮助,我将不胜感激。

标签: node.js

解决方案


正如@pzaenger 指出的.toString那样.toString()

编辑:

这里的关键是学会阅读你的堆栈跟踪。

buffer.js:778                                       // This is a node module
    return this.utf8Slice(0, this.length);          
                ^

TypeError: Cannot read property 'utf8Slice' of undefined  // This is sort of helpful, it's telling use something is trying to access a function utf8Slice of undefined
    at toString (buffer.js:778:17)                        // buffer.js is trying to access utf8Slice on a this object at `toString`
                                                          // So now we know a buffer is trying to call utf8Slice on an undefined `this`
                                                          // at `toString`. Taking a quick look at your code we see fs.readFileSync(...)
                                                          // which gives us a buffer back and then `fs.readFileSync(...).toString` 
                                                          // experince tells us that toString is a function not a property

推荐阅读