My terminal is throwing an error that seems as clear as mud:
TSError: ⨯ Unable to compile TypeScript: [orders-depl-9fbcb84ff-ngt2z orders] src/models/ticket.ts(47,5): error TS2322: Type 'Document' is not assignable to type 'Pick<Pick<_LeanDocument, "_id" | "__v" | "id" | "title" | "price" | "isReserved">, "_id" | "__v" | "id" | "title" | "price"> | QuerySelector<...> | undefined'. [orders-depl-9fbcb84ff-ngt2z orders] Type 'Document' is missing the following properties from type 'Pick<Pick<_LeanDocument, "_id" | "__v" | "id" | "title" | "price" | "isReserved">, "_id" | "__v" | "id" | "title" | "price">': title, price
It's pointing to this model file:
import mongoose from 'mongoose';
import { Order, OrderStatus } from './order';
interface TicketAttrs {
title: string;
price: number;
}
export interface TicketDoc extends mongoose.Document {
title: string;
price: number;
isReserved(): Promise<boolean>;
}
interface TicketModel extends mongoose.Model<TicketDoc> {
build(attrs: TicketAttrs): TicketDoc;
}
const ticketSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
price: {
type: Number,
required: true,
min: 0
}
}, {
toJSON: {
transform(doc, ret) {
ret.id = ret._id;
delete ret._id;
}
}
});
ticketSchema.statics.build = (attrs: TicketAttrs) => {
return new Ticket(attrs);
};
// Run query to look at all orders. Find an order where the
// ticket is the ticket just found *and* the order status is *not* cancelled.
// If we find an order from that means the ticket *is* reserved
ticketSchema.methods.isReserved = async function () {
// this === the ticket document that I just called 'isReserved' on
const existingOrder = await Order.findOne({
ticket: this,
status: {
$in: [
OrderStatus.Created,
OrderStatus.AwaitingPayment,
OrderStatus.Complete
]
}
});
return !!existingOrder;
};
const Ticket = mongoose.model<TicketDoc, TicketModel>('Ticket', ticketSchema);
export { Ticket };
I can't spot any syntax errors and this Pick
type baffles me. It appears that the problem lies in ticket
not being equal to this
, although it should be.