The issue I’m facing is that despite observing traffic on my MongoDB database, the products I expect to be stored within it are not present. I scrape product data from Amazon and attempt to store it in the database using a defined schema and model. Despite successful scraping and apparent database activity, the products don’t appear in the database collection.
Note that in the terminal, despite successful scraping, I still receive an error message that I’m unable to resolve. This error may be a contributing factor to the issue at hand.
Here is some of my code :
"use server"
import Product from "../models/product.model";
import { connectTodb } from "../mongoose";
import { scrapeAmazonProduct } from "../scraper";
import { getAveragePrice, getHighestPrice, getLowestPrice } from "../util";
import { revalidatePath } from "next/cache";
export async function scrapeAndStoreProduct(productURL: string) {
if (!productURL) return;
try {
connectTodb();
const scrapedProduct: any = await scrapeAmazonProduct(productURL);
if (!scrapedProduct) {
console.log("Failed to scrape product");
return; // Stop execution if product scraping failed
}
let product = scrapedProduct;
// Check if scrapedProduct is an object and has a url property
if (typeof scrapedProduct === 'object' && scrapedProduct !== null && 'url' in scrapedProduct) {
const existingProduct = await Product.findOne({ url: scrapedProduct.url });
if (existingProduct) {
const updatedPriceHistory : any = [
...existingProduct.priceHistory,
{ price: scrapedProduct.currentPrice }
]
product = {
...scrapedProduct,
priceHistory: updatedPriceHistory,
lowestPrice: getLowestPrice(updatedPriceHistory),
highestPrice: getHighestPrice(updatedPriceHistory),
averagePrice: getAveragePrice(updatedPriceHistory)
}
}
}
const newProduct = await Product.findOneAndUpdate(
{url: scrapedProduct.url},
product,
{upsert: true, new:true}
);
revalidatePath(`/products/${newProduct._id}`);
} catch (error: any) {
throw new Error(`Failed to create/update product: ${error.message}`)
}
}
export async function getProductById(productId: string){
try {
connectTodb();
} catch (error) {
}
}
import mongoose from "mongoose";
import { connectTodb } from "../mongoose";
const productSchema = new mongoose.Schema({
url: {type : String, required:true, unique:true},
currency: {type: String, required:true},
image: {type: String, required:true},
title: {type: String, required:true},
currentPrice: {type: Number, required:true},
originalPrice: {type: Number, required:true},
priceHistory: [
{
prices: {type: Number, required: true},
date: {type:Date, default: Date.now}
},
],
lowestPrice: {type: Number},
highestPrice: {type: Number},
averagePrice: {type: Number},
discountRate: {type: Number},
description: {type: String},
isOutOfStock: {type: Boolean, default: false},
users: [{email: {type: String, required: true}}],
}, {timestamps: true});
const Product = mongoose.models.Product || mongoose.model('Product', productSchema);
export default Product;
import mongoose from "mongoose";
let isConnected = false;
export const connectTodb = async () => {
mongoose.set('strictQuery', true);
// Vérifie si MONGODB_URI est défini
if (!process.env.MONGODB_URI) {
console.error('MONGODB_URI is not defined');
return;
}
if(isConnected) return console.log('=> using existing database connection');
try {
await mongoose.connect(process.env.MONGODB_URI);
isConnected = true;
console.log('MONGODB Connected');
} catch (error) {
console.error('Failed to connect to MongoDB:', error);
}
};