const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
name: {
type: String,
required: true // Ensures name is provided
},
price: {
type: mongoose.Types.Decimal128,
required: true // Ensures price is provided
}
});
module.exports = mongoose.model('Item', itemSchema);
const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
name: {
type: String,
required: true // Ensures name is provided
},
price: {
type: Number,
required: true // Ensures price is provided
}
});
module.exports = mongoose.model('Item', itemSchema);
// Add this endpoint to get the total
router.get('/total', async (req, res) => {
try {
const items = await Item.find();
const total = items.reduce((acc, item) => acc + item.price, 0); // Calculate the total
res.json({ total: total.toFixed(2) }); // Send back a nicely formatted string
} catch (error) {
res.status(500).send(error);
}
});
<!DOCTYPE html>
<html lang="en">
<head>