Node / Express with Graphql错误“_author2.default不是构造函数”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node / Express with Graphql错误“_author2.default不是构造函数”相关的知识,希望对你有一定的参考价值。

我一直在寻找这个错误,但没有取得成功的解决方案。我正在学习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"
  }

我的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;

我的resolvers.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;

一切正常,我的表达式可以正常使用apollo-server-express,当我使用addAuthor变异时,调用解析器中的函数addAuthor,但是在创建作者objetc模型时出现错误,错误:

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)

我不明白为什么会发生这种错误,提前谢谢。

答案

试试这个:

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文件import authorModel from './model/author';意味着必须从authorModel文件中导出author.js。但看起来并非如此。从authorModel文件中导出author.js

以上是关于Node / Express with Graphql错误“_author2.default不是构造函数”的主要内容,如果未能解决你的问题,请参考以下文章

Node.js / Express with vhost 与 Sails.js 框架应用程序冲突

Author name disambiguation using a graph model with node splitting and merging based on bibliographi

Node / Express with Graphql错误“_author2.default不是构造函数”

Find all paths with min weights in an undirected graph

我无法将数据存储到 Postman 上的 MongoDB(Atlas)中(Node.js with express)

node express 跨域问题