Javascript GraphQL错误-this.findById不是函数
嗨,伙计们,我正试图通过使用变异为我的歌曲添加歌词,但它返回了一个错误Javascript GraphQL错误-this.findById不是函数,javascript,reactjs,graphql,schema,graphiql,Javascript,Reactjs,Graphql,Schema,Graphiql,嗨,伙计们,我正试图通过使用变异为我的歌曲添加歌词,但它返回了一个错误this.findById不是一个函数。变异格式是正确的,因为我遵循一个教程,但它相当古老,所以我不确定这是否是一个因素 song_type.js const mongoose = require('mongoose'); const graphql = require('graphql'); const { GraphQLObjectType, GraphQLString, GraphQLID, Graph
this.findById不是一个函数
。变异格式是正确的,因为我遵循一个教程,但它相当古老,所以我不确定这是否是一个因素
song_type.js
const mongoose = require('mongoose');
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString,
GraphQLID,
GraphQLList
} = graphql;
const LyricType = require('./lyric_type');
const Song = mongoose.model('song');
const SongType = new GraphQLObjectType({
name: 'SongType',
fields: () => ({
id: {
type: GraphQLID
},
title: {
type: GraphQLString
},
lyrics: {
type: new GraphQLList(LyricType),
resolve(parentValue) {
return Song.findLyrics(parentValue.id);
}
}
})
});
module.exports = SongType;
js
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString,
GraphQLID
} = graphql;
const mongoose = require('mongoose');
const Song = mongoose.model('song');
const Lyric = mongoose.model('lyric');
const SongType = require('./song_type');
const LyricType = require('./lyric_type');
const mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addSong: {
type: SongType,
args: {
title: {
type: GraphQLString
}
},
resolve(parentValue, {
title
}) {
return (new Song({
title
})).save()
}
},
addLyricToSong: {
type: SongType,
args: {
content: {
type: GraphQLString
},
songId: {
type: GraphQLID
}
},
resolve(parentValue, {
content,
songId
}) {
return Song.addLyric(songId, content);
}
},
likeLyric: {
type: LyricType,
args: {
id: {
type: GraphQLID
}
},
resolve(parentValue, {
id
}) {
return Lyric.like(id);
}
},
deleteSong: {
type: SongType,
args: {
id: {
type: GraphQLID
}
},
resolve(parentValue, {
id
}) {
return Song.remove({
_id: id
});
}
}
}
});
module.exports = mutation;
lyric_type.js
const mongoose = require('mongoose');
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLList,
GraphQLID,
GraphQLInt,
GraphQLString
} = graphql;
const Lyric = mongoose.model('lyric');
const LyricType = new GraphQLObjectType({
name: 'LyricType',
fields: () => ({
id: {
type: GraphQLID
},
likes: {
type: GraphQLInt
},
content: {
type: GraphQLString
},
song: {
type: require('./song_type'),
resolve(parentValue) {
return Lyric.findById(parentValue).populate('song')
.then(lyric => {
console.log(lyric)
return lyric.song
});
}
}
})
});
module.exports = LyricType;
不确定是否有足够的文件来解决这个错误,如果没有完整的回购-问题是,虽然我不确定为什么
这个没有定义,因为您定义的是文档中所示的静态函数。我想findById
在模式中不可用,但在模型中(如lyric.js
)。。。您应该能够使用this.find({id})
有趣的是,突变正在经历。。。我对那首歌有很多“测试”的歌词:D问题是,虽然我不知道为什么这个
没有定义,因为你正在定义文档中显示的静态函数。我想findById
在模式中不可用,但在模型中(如lyric.js
)中。。。您应该能够使用this.find({id})
有趣的是,突变正在经历。。。我有很多“测试”这首歌的歌词:D