Within a function, I am manipulating the properties of an object of type CardType
. I need to update these changes in my MongoDB database using the document.save()
function. However, I have encountered an issue with my code:
import { Types } from 'mongoose';
import Card from '../models/Card';
static async updatePriceForCardByID(cardID: Types.ObjectId) {
const card = await Card.findById(cardID).exec();
return updatePriceForCard(card!);
}
static async updatePriceForCard(card: CardType) {
// This function requires certain properties of 'card' and updates the price of the card
const newPrice = await StarCityController.getUpdatedPriceForCard(card.name, card.number);
card.prices.push(newPrice);
card.save(); // <-- An error occurs here
return Card.findById(card._id).populate({ path: 'prices' }).exec();
}
The definition for my CardType
looks like this:
import { Types } from 'mongoose';
export type CardType = {
_id: Types.ObjectId,
name: string,
prices: [Types.ObjectId],
number: string,
sku: string
}
The issue lies in the fact that the card.save()
function is not functioning as expected since I am now working with TypeScript and passing a CardType
parameter instead of a mongoose Document.
What would be the most effective way to handle this situation?