mongo 数据库数据未在 graphql 的解析器函数中返回,但在我的 nodejs 服务器的路由中工作
mongo db data not returned in resolver function for graphql but working in routes of my nodejs server
我正在设置一个基于 hapi、graphql 和 mongodb 的节点 js 环境。我能够连接到 mongodb 并使用 db mongoose 模式在 GET/POST 路由中检索和显示数据。但是当模型传递给 graphql 的解析器函数时,数据没有被检索到。请在下面找到我的 graphql 解析器函数
const resolvers = (shows)=>({
myQuery:{
testFunction(){
return "returned from custom test function";
},
getShowByName: function(_,args){
var out= shows.findOne();
console.log(out); //returning a huge json response instead of proper data
return out={
_id:"5349b4ddd2781d08c09890f3",
title: "test",
version: "test",
showDetails: [{
name: args.showSchemaName,
genre: "test",
lead_actor: "test"
}]
}
;
},
},
myMutation: {
createShow: function(_,args){
return args.showTypeInputDetails.title+","+args.showTypeInputDetails.version;
}
}
});
module.exports = resolvers;
Console.log(out) 发出了一个巨大的 json 响应,该响应并非来自 mongo 数据库。 json 响应非常大,还有我的连接参数、凭据和其他详细信息,因此,我在这里发布响应的开头
Query {
_mongooseOptions: {},
_transforms: [],
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts:
{ bufferCommands: true,
capped: false,
'$wasForceClosed': undefined },
name: 'shows_details',
collectionName: 'shows_coll',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
请帮助我理解为什么当从解析器函数触发 findOne() 时会出现此响应,而当从路由函数触发相同函数时会出现正确结果。
[显示是我的 mongoose db 模型]
你为什么不试试下面的代码。
dbo.collection("collection_name").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
我让我的解析器函数异步,并用 await 返回数据并解决了问题
我正在设置一个基于 hapi、graphql 和 mongodb 的节点 js 环境。我能够连接到 mongodb 并使用 db mongoose 模式在 GET/POST 路由中检索和显示数据。但是当模型传递给 graphql 的解析器函数时,数据没有被检索到。请在下面找到我的 graphql 解析器函数
const resolvers = (shows)=>({
myQuery:{
testFunction(){
return "returned from custom test function";
},
getShowByName: function(_,args){
var out= shows.findOne();
console.log(out); //returning a huge json response instead of proper data
return out={
_id:"5349b4ddd2781d08c09890f3",
title: "test",
version: "test",
showDetails: [{
name: args.showSchemaName,
genre: "test",
lead_actor: "test"
}]
}
;
},
},
myMutation: {
createShow: function(_,args){
return args.showTypeInputDetails.title+","+args.showTypeInputDetails.version;
}
}
});
module.exports = resolvers;
Console.log(out) 发出了一个巨大的 json 响应,该响应并非来自 mongo 数据库。 json 响应非常大,还有我的连接参数、凭据和其他详细信息,因此,我在这里发布响应的开头
Query {
_mongooseOptions: {},
_transforms: [],
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts:
{ bufferCommands: true,
capped: false,
'$wasForceClosed': undefined },
name: 'shows_details',
collectionName: 'shows_coll',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
请帮助我理解为什么当从解析器函数触发 findOne() 时会出现此响应,而当从路由函数触发相同函数时会出现正确结果。
[显示是我的 mongoose db 模型]
你为什么不试试下面的代码。
dbo.collection("collection_name").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
我让我的解析器函数异步,并用 await 返回数据并解决了问题