Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Graphql查询始终返回null_Node.js_Express_Graphql_Express Graphql - Fatal编程技术网

Node.js Graphql查询始终返回null

Node.js Graphql查询始终返回null,node.js,express,graphql,express-graphql,Node.js,Express,Graphql,Express Graphql,我试图使用graphql获取课程数据,但服务器总是返回null作为响应这是我在文件server.js中的代码: var express=require('express'); const { graphqlHTTP } = require('express-graphql') var {buildSchema}=require('graphql'); //graphql schema var schema=buildSchema(` type Query { course(id: In

我试图使用graphql获取课程数据,但服务器总是返回
null
作为响应这是我在文件
server.js中的代码:

var express=require('express');
const { graphqlHTTP } = require('express-graphql')
var {buildSchema}=require('graphql');

//graphql schema
var schema=buildSchema(`
type Query {
    course(id: Int!): Course
    courses(topic:String!):[Course]

}
type Course {
    id: Int
    title: String
    author: String
    description:String
    topic:String
    url: String
}
`);

var coursesData = [
    {
        id: 1,
        title: 'The Complete Node.js Developer Course',
        author: 'Andrew Mead, Rob Percival',
        description: 'Learn Node.js by building real-world applications with Node, Express, MongoDB, Mocha, and more!',
        topic: 'Node.js',
        url: 'https://codingthesmartway.com/courses/nodejs/'
    },
    {
        id: 2,
        title: 'Node.js, Express & MongoDB Dev to Deployment',
        author: 'Brad Traversy',
        description: 'Learn by example building & deploying real-world Node.js applications from absolute scratch',
        topic: 'Node.js',
        url: 'https://codingthesmartway.com/courses/nodejs-express-mongodb/'
    },
    {
        id: 3,
        title: 'JavaScript: Understanding The Weird Parts',
        author: 'Anthony Alicea',
        description: 'An advanced JavaScript course for everyone! Scope, closures, prototypes, this, build your own framework, and more.',
        topic: 'JavaScript',
        url: 'https://codingthesmartway.com/courses/understand-javascript/'
    }
]

//root resolver
var root= {
    course:getCourse,
    courses:getCourses
};


var getCourse= function (args){
    var id=args.id;
    console.log("delila")
    return coursesData.filter(course=>{
        return course.id==id
    })[0]

}
var getCourses = function(args){
    if(args.topic){
        var topic=args.topic;
        return coursesData.filter(course=>
            course.topic===topic
        );

    }
    else return coursesData
    
}
//create an experess server and graphql endpoint
var app=express();
app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue:root,
    graphiql:true
}));
app.listen(4000,()=>console.log("delila express graphql server running on localhost 4000"))
当我转到
localhost:4000/graphql
获取我正在使用的数据时

query getSingleCourse($courseID: Int !){
  course(id:$courseID){
    title
    author
    description
    url
    topic
  }
}

{
  "courseID": 3
}
但我不断得到结果
null
。看图像


有人知道为什么会这样吗?服务器应该返回带有
ID3
的过程,但显然我缺少了一些东西

您应该首先定义函数表达式,然后使用它们。这就是原因

与函数声明不同,JavaScript中的函数表达式不会被提升。在创建函数表达式之前,不能使用它们:

例如

/。。。
var getCourse=函数(args){
var id=args.id;
console.log('delila');
返回coursesData.filter((课程)=>{
return course.id==id;
})[0];
};
var getCourses=函数(args){
if(args.topic){
var topic=args.topic;
返回coursesData.filter((course)=>course.topic===topic);
}否则返回课程数据;
};
//根解析器
变量根={
课程:getCourse,
课程:getCourses,
};
//...

“args”是第二个解析器fn arg