-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-product-deletion.js
More file actions
44 lines (36 loc) · 1.37 KB
/
test-product-deletion.js
File metadata and controls
44 lines (36 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const mongoose = require('mongoose');
const Product = require('./models/Product');
require('dotenv').config();
async function testProductDeletion() {
try {
await mongoose.connect(process.env.MONGODB_URI);
console.log('Connected to MongoDB');
// Get all products
const products = await Product.find({}).select('_id name isActive');
console.log('\n=== Products in database ===');
products.forEach(p => {
console.log(`ID: ${p._id}`);
console.log(`Name: ${p.name}`);
console.log(`Active: ${p.isActive}`);
console.log('---');
});
if (products.length > 0) {
const testId = products[0]._id;
console.log(`\n=== Testing deletion of product: ${testId} ===`);
// Test if we can find it
const foundProduct = await Product.findById(testId);
console.log('Found with findById:', !!foundProduct);
const foundProduct2 = await Product.findOne({ _id: testId });
console.log('Found with findOne:', !!foundProduct2);
// Test ObjectId validation
console.log('Is valid ObjectId:', mongoose.Types.ObjectId.isValid(testId.toString()));
console.log('ID string:', testId.toString());
console.log('ID length:', testId.toString().length);
}
process.exit(0);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
testProductDeletion();