【发布时间】:2019-05-27 17:48:34
【问题描述】:
我正在尝试使用Mongoose 和GraphQL 建立一对多关系数据库。
每当我将数据插入 GraphQL 变异参数时,我都会收到 [Object: null prototype] 错误。
当我尝试使用console.log 进行调试时,我注意到该对象前面会有[Object: null prototype]。
我尝试了很多方法,尝试map() args 甚至使用replace(),但没有运气。我得到的只是"args.ingredient.map/replace is not a function"
我已经通过更改参数来测试硬编码方法,例如:
args.category = '5c28c79af62fad2514ccc788'
args.ingredient = '5c28c8deb99a9d263462a086'
令人惊讶的是,它适用于这种方法。我假设输入不能是一个对象,而只是一个 ID。
请参阅下面的实际结果。
解析器
Query: {
recipes: async (root, args, { req }, info) => {
return Recipe.find({}).populate('ingredient category', 'name createdAt').exec().then(docs => docs.map(x => x))
},
},
Mutation: {
addRecipe: async (root, args, { req }, info) => {
// args.category = '5c28c79af62fad2514ccc788'
// args.ingredient = '5c28c8deb99a9d263462a086'
// console.log(args.map(x => x))
return Recipe.create(args)
}
}
类型定义
extend type Mutation {
addRecipe(name: String!, direction: [String!]!, ingredient: [IngredientInput], category: [CategoryInput]): Recipe
}
type Recipe {
id: ID!
name: String!
direction: [String!]!
ingredient: [Ingredient!]!
category: [Category!]!
}
input IngredientInput {
id: ID!
}
input CategoryInput {
id: ID!
}
型号
const recipeSchema = new mongoose.Schema({
name: String,
direction: [String],
ingredient: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Ingredient' }],
category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }
}, {
timestamps: true // createdAt, updateAt
})
const Recipe = mongoose.model('Recipe', recipeSchema)
这是我在插入数据时控制台记录 args 的结果
{
name: 'Butter Milk Chicken TEST2',
direction: [ 'Step1', 'Step2', 'Step3' ],
ingredient:[[Object: null prototype] { id: '5c28c8d6b99a9d263462a085' }],
category: [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }]
}
我想我需要得到这样的东西
{
name: 'Butter Milk Chicken TEST2',
direction: [ 'Step1', 'Step2', 'Step3' ],
args.category = ['5c28c79af62fad2514ccc788']
args.ingredient = ['5c28c8ccb99a9d263462a083', '5c28c8d3b99a9d263462a084', '5c28c8d6b99a9d263462a085']
}
【问题讨论】:
-
数组从何而来?
-
Recipe.create行中的“get [Object: null prototype] error”到底在哪里?您能否发布完整的错误消息,请 -
我在 apollo 项目中作为问题提交:github.com/apollographql/apollo-server/issues/3149
-
这已在 Firestore 中很快得到修复。 github.com/googleapis/nodejs-firestore/pull/736
标签: javascript node.js mongoose graphql apollo-server