我有一個帶有 MongoDB 資料庫的應用程式。我有一個包含客戶和門票的架構,一切正常。我正在嘗試添加一個材料集合,我已將其添加到我的架構中,但我不斷收到此錯誤:
錯誤:材質欄位必須是欄位名稱作為鍵的物件或回傳此類物件的函式。
我試過搜索,沒有看到任何與我的完全一樣的問題,我看到有人遇到了同樣的錯誤,但他是一個錯字,我的代碼中沒有看到任何錯字。不代表沒有,只是我沒看到。除了錯誤訊息外,我的服務器似乎崩潰了。我無法通過 http://localhost:5400/graphql 訪問 GraphiQL
如果我注釋掉我的材料條目,一切都會再次正常作業。
這是我的架構:
const Customer = require('../models/Customer');
const Ticket = require('../models/Ticket');
const Material = require('../models/Material');
const { GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLInt,
GraphQLNonNull,
GraphQLDeprecatedDirective,
} = require('graphql');
//Tickets
const TicketType = new GraphQLObjectType({
name: 'Ticket',
fields: () => ({
id: { type: GraphQLID },
date: { type: GraphQLString},
ticketNum: { type: GraphQLString},
customer: {
type: CustomerType,
resolve(parent, args) {
return Customer.findById(parent.customerId);
},
},
material: { type: GraphQLString},
tareWeight: { type: GraphQLInt},
grossWeight: { type: GraphQLInt},
netWeight: { type: GraphQLInt},
notes: { type: GraphQLString},
}),
});
//Materials
const MaterialType = new GraphQLObjectType({
name: "Material",
Fields: () => ({
id: { type: GraphQLID },
matId: { type: GraphQLString},
name: { type: GraphQLString },
price: { type: GraphQLInt },
unit: { type: GraphQLString},
notes: { type: GraphQLString,}
}),
});
//Customers
const CustomerType = new GraphQLObjectType({
name: "Customer",
fields: () => ({
id: { type: GraphQLID },
custId: { type: GraphQLString},
name: { type: GraphQLString},
street: { type: GraphQLString},
city: { type: GraphQLString},
zip: { type: GraphQLString},
webSite: { type: GraphQLString},
email: { type: GraphQLString},
phone: { type: GraphQLString},
}),
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
tickets: {
type: new GraphQLList(TicketType),
resolve(parent, args) {
return Ticket.find();
},
},
ticket: {
type: TicketType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return Ticket.findById(args.id);
},
},
customers: {
type: new GraphQLList(CustomerType),
resolve(parent, args) {
return Customer.find();
},
},
customer: {
type: CustomerType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return Customer.findById(args.id);
},
},
materials: {
type: new GraphQLList(MaterialType),
resolve(parent, args) {
return Material.find();
},
},
material: {
type: MaterialType,
args: {id: { type: GraphQLID } },
resolve(parent, args) {
return Material.findById(args.id);
},
},
},
});
//Mutations
const mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
//Add Customer
addCustomer: {
type: CustomerType,
args: {
custId: { type: GraphQLString},
name: { type: GraphQLString},
street: { type: GraphQLString},
city: { type: GraphQLString},
zip: { type: GraphQLString},
webSite: { type: GraphQLString},
email: { type: GraphQLString},
phone: { type: GraphQLString},
},
resolve(parent, args) {
const customer = new Customer({
custId: args.custId,
name: args.name,
street: args.street,
city: args.city,
zip: args.zip,
webSite: args.webSite,
email: args.email,
phone: args.phone,
});
return customer.save();
}
},
// //Add Material
addMaterial: {
type: MaterialType,
args: {
matId: { type: GraphQLString},
name: { type: GraphQLString},
price: { type: GraphQLString},
unit: { type: GraphQLString},
notes: { type: GraphQLString},
},
resolve(parent, args) {
const material = new Material({
matId: args.matId,
name: args.name,
price: args.price,
unit: args.unit,
notes: args.notes,
});
return material.save();
}
},
// Delete Customer
deleteCustomer: {
type: CustomerType,
args: {
id: { type: GraphQLNonNull(GraphQLID) },
},
resolve(parent, args) {
return Customer.findByIdAndRemove(args.id);
},
},
// Add Ticket
addTicket: {
type: TicketType,
args: {
date: { type: GraphQLNonNull(GraphQLString) },
ticketNum: { type: GraphQLNonNull(GraphQLString) },
customerId: { type: GraphQLNonNull(GraphQLID) },
material: { type: GraphQLNonNull(GraphQLString) },
tareWeight: { type: GraphQLNonNull(GraphQLInt) },
grossWeight: { type: GraphQLNonNull(GraphQLInt) },
netWeight: { type: GraphQLNonNull(GraphQLInt) },
notes: { type: GraphQLNonNull(GraphQLString) },
},
resolve(parent, args) {
const ticket = new Ticket({
date: args.date,
ticketNum: args.ticketNum,
customerId: args.customerId,
material: args.material,
tareWeight: args.tareWeight,
grossWeight: args.grossWeight,
netWeight: args.netWeight,
notes: args.notes,
});
return ticket.save();
}
},
//Delete a Ticket
deleteTicket: {
type: TicketType,
args: {
id: { type: GraphQLNonNull(GraphQLID) },
},
resolve(parent, args) {
return Ticket.findByIdAndRemove(args.id);
},
},
// Update Ticket
updateTicket: {
type: TicketType,
args: {
id: { type: GraphQLNonNull(GraphQLID) },
date: { type: GraphQLString },
ticketNum: { type: GraphQLString },
// customerId: { type: GraphQLID },
material: { type: GraphQLString },
tareWeight: { type: GraphQLInt },
grossWeight: { type: GraphQLInt },
netWeight: { type: GraphQLInt },
notes: { type: GraphQLString },
},
resolve(parent, args) {
return Ticket.findByIdAndUpdate(
args.id,
{
$set: {
date: args.date,
ticketNum: args.ticketNum,
// customerId: args.customerId,
material: args.material,
tareWeight: args.tareWeight,
grossWeight: args.grossWeight,
netWeight: args.netWeight,
notes: args.notes,
},
},
{ new: true }
);
},
},
},
});
module.exports = new GraphQLSchema({
query: RootQuery,
mutation
});
有人知道我在做什么錯嗎?
謝謝,
內特
uj5u.com熱心網友回復:
你在其他地方Fields有一個大寫字母,小寫字母。FMaterials
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/513840.html
下一篇:設定無線電組檢查狀態
