我正在尝试更新“库存”集合中的文档。我传递了库存ID和应另存为新库存的newItems。
EDIT 我知道,还有另一个问题提到“ Cast to Array”-为该问题选择的答案涉及不需要使用“ type”作为关键。我没有使用“类型”作为键。
当我尝试运行测试时,例如:
mutation{
updateInventory(id:"5d72e794a00da51083c56615",newItems:[{name:"Test", quantity:3},{name:"Test2",quantity:3}]){
items{
name
}
}
}
我得到了错误:
"{
"errors": [
{
"message": "Inventory validation failed: items: Cast to Array failed for value \"[ [Object: null prototype] { name: 'Test', quantity: 3 },\n [Object: null prototype] { name: 'Test2', quantity: 3 } ]\" at path \"items\"","
任何关于我要去哪里的指示都值得赞赏!
代码段:
解析器-
updateInventory: async (parent, args) => {
let selectedInventory = await Inventory.findById(args.id)
selectedInventory.items = [...args.newItems]
await selectedInventory.save()
return selectedInventory
}
相关TypeDefs-
type Item{
name: String!
quantity: Int!
}
input ItemInput{
name: String!
quantity: Int!
}
type Inventory{
items: [Item]!
}
type Mutation{
updateInventory(id: String!, newItems: [ItemInput]!): Inventory!
}