Express 带有Graphql错误的节点/表达式“_author2.default不是构造函数;

Express 带有Graphql错误的节点/表达式“_author2.default不是构造函数;,express,graphql,apollo-server,Express,Graphql,Apollo Server,我一直在寻找这个错误,但没有找到成功的解决方案。我正在学习graphql,所以我构建了一个小应用程序,下面是我的package.json: "dependencies": { "apollo-server-express": "^1.3.6", "babel-watch": "^2.0.7", "body-parser": "^1.18.3", "express": "^4.16.3", "graphql": "^0.13.2", "graphql

我一直在寻找这个错误,但没有找到成功的解决方案。我正在学习graphql,所以我构建了一个小应用程序,下面是我的package.json:

"dependencies": {
    "apollo-server-express": "^1.3.6",
    "babel-watch": "^2.0.7",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "graphql": "^0.13.2",
    "graphql-tools": "^3.0.2",
    "mongoose": "^5.1.3",
    "node-uuid": "^1.4.8"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0"
  }
My schema.js:

import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'
import resolvers from './resolvers'

const typeDefs = `
    type Author {
        id: String
        name: String
        age: Int
        books: [String]
    }

    type Query {
        authors: [Author]
        author(id: String): Author
    }

    type Mutation {
        addAuthor(name: String!, age: Int!, books: [String]!):Author
    }
`
const schema = makeExecutableSchema({typeDefs, resolvers});

export default schema;
My Resolver.js:

import mongoose from 'mongoose';
import authorModel from './model/author';

const resolvers = {
    Query: {
        authors: () => {
            //return fakeData;
        },
        // root is never used, and args holds our filter params
        author: (root, args) => {
            //const id = args.id;
            //return fakeData.find((author) => author.id === id);
        }
    },
    // mutations changes the data states
    Mutation: {
        addAuthor: (root, {name, age, books}) => {
            console.log(name, age, books);
            const author = new authorModel({name: name, age: age, books: books});
            return author.save();
        }
    }
};

export default resolvers;
最后,我的模型author.js:

import mongoose from 'mongoose';
// to generate our unique IDs
import uuid from 'node-uuid';
const schema = mongoose.Schema;

// this schema must be compatible with schema from graphQl
const authorSchema = new schema({
    id: {type: String, default: uuid.v1},
    name: String,
    age: Number,
    books: [String]
});

export default authorSchema;
All works ok,my express与apollo server express配合使用正常,当我使用addAuthor变体时,会调用resolver中的函数addAuthor,但在创建author对象模型时发生错误,错误为:

TypeError: _author2.default is not a constructor
    at addAuthor (/home/desinv04/estudos/graphql/worldcup-graphql/resolvers.js:46:28)
    at resolveFieldValueOrError (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:531:18)
    at resolveField (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:495:16)
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:339:18
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:25:10
    at Array.reduce (<anonymous>)
    at promiseReduce (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:22:17)
    at executeFieldsSerially (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:336:38)
    at executeOperation (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:289:55)
    at executeImpl (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:154:14)
    at Object.execute (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:131:229)
    at doRunQuery (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:194:7)
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:79:39
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)
TypeError:\u author2.default不是构造函数
在addAuthor(/home/desinv04/estudos/graphql/worldcup graphql/resolvers.js:46:28)
在resolveFieldValueOrError(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:531:18)
在resolveField(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:495:16)
at/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:339:18
at/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/jsutils/promiseReduce.js:25:10
在Array.reduce()处
在promiseReduce(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/jsutils/promiseReduce.js:22:17)
在ExecuteFieldsSerial(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:336:38)
在执行操作时(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:289:55)
在executeImpl(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:154:14)
在Object.execute(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/graphql/execution/execute.js:131:229)
在doRunQuery(/home/desinv04/estudos/graphql/worldcup graphql/node_modules/apollo server core/src/runQuery.ts:194:7)
at/home/desinv04/estudos/graphql/worldcup graphql/node_modules/apollo server core/src/runQuery.ts:79:39
在
在进程中。_tick回调(internal/process/next_tick.js:182:7)
我不明白为什么会发生此错误,请提前感谢。

尝试以下操作:

import mongoose from 'mongoose';
import uuid from 'node-uuid';
const schema = mongoose.Schema;

const authorSchema = new schema({
    id: {type: String, default: uuid.v1},
    name: String,
    age: Number,
    books: [String]
})

const model = mongoose.model('author', authorSchema);
export default model;

引用您的
resolvers.js
文件
从“/model/author”导入authorModel
表示必须从文件
author.js
导出
authorModel
。但看起来不是。从
author.js
文件导出
authorModel

我认为在mongoose中,模式不是模型。您必须首先创建模型:
导出默认mongoose.model(authorSchema)
。请参见文档中的: