node.js - 为什么我会收到此错误 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)
我需要帮助,我将不胜感激。
解决方案
正如@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
推荐阅读
- ios - iOS 13 后台杀
- azure - 部署到 Azure 后无法将源内容类型 Umbraco.Web.PublishedCache.NuCache.PublishedContent 绑定到模型类型
- python - 使用 Python 向 Microsoft Teams 发送自动消息
- forms - 表单中的 PowerShell 弹出窗口
- inno-setup - Inno Setup:强制组件始终具有与其父级相同的检查状态
- redux - 如何通过键名删除 redux 存储
- tensorflow - 我的 tensorflow 2.0 自定义模型没有收到我期望的形状或值
- image - 图片库保护程序锁主线程flutter
- regex - 正则表达式查找数字和单词之间缺少空格的位置
- c++ - C++ 指向另一个类的指针的动态数组