- passato mongoose da versione 6 a versione 7

This commit is contained in:
Surya Paolo
2025-03-03 00:59:13 +01:00
parent 53a70a1c96
commit 0a4cea94ae
5 changed files with 70 additions and 916 deletions

View File

@@ -89,35 +89,41 @@ module.exports.getCartByUserId = async function (uid, idapp) {
};
module.exports.updateCartByUserId = async function (userId, newCart, callback) {
let query = { userId: userId };
try {
const c = await Cart.find(query);
} catch (err) {
if (err) throw err;
}
module.exports.updateCartByUserId = async function (userId, newCart) {
const query = { userId: userId };
//exist cart in databse
if (c.length > 0) {
Cart.findOneAndUpdate(
{ userId: userId },
{
$set: {
items: newCart.items,
totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice,
totalPriceCalc: newCart.totalPriceCalc,
userId: userId,
try {
// Cerca il carrello esistente nel database
const existingCart = await Cart.findOne(query);
if (existingCart) {
// Se il carrello esiste, aggiorna i dati
const updatedCart = await Cart.findOneAndUpdate(
query,
{
$set: {
items: newCart.items,
totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice,
totalPriceCalc: newCart.totalPriceCalc,
userId: userId,
},
},
},
{ new: true },
callback,
);
} else {
//no cart in database
newCart.save(callback);
{ new: true } // Restituisce il documento aggiornato
);
return updatedCart; // Restituisce il carrello aggiornato
} else {
// Se il carrello non esiste, crea un nuovo documento
const createdCart = new Cart(newCart);
const savedCart = await createdCart.save();
return savedCart; // Restituisce il carrello creato
}
} catch (err) {
// Gestione degli errori
console.error("Errore durante l'aggiornamento del carrello:", err);
throw err; // Propaga l'errore al chiamante
}
};
module.exports.updateCartByCartId = async function (cartId, newCart) {

View File

@@ -1,7 +1,7 @@
var mongoose = require('mongoose').set('debug', false)
var mongoose = require('mongoose').set('debug', false)
var variantSchema = mongoose.Schema({
var variantSchema = mongoose.Schema({
productID: {
type: String
},
@@ -27,18 +27,24 @@ var variantSchema = mongoose.Schema({
var Variant = module.exports = mongoose.model('Variant', variantSchema);
module.exports.getVariantByID = function(id, callback){
module.exports.getVariantByID = function (id, callback) {
Variant.findById(id, callback);
}
module.exports.getVariantProductByID = function(id, callback){
var query = {productID: id};
module.exports.getVariantProductByID = function (id, callback) {
var query = { productID: id };
Variant.find(query, callback);
}
module.exports.getAllVariants = function(callback){
module.exports.getAllVariants = function (callback) {
Variant.find(callback)
}
module.exports.createIndexes((err) => {
if (err) throw err;
});
module.exports.createIndexes = function () {
return this.createIndexes()
.then(() => {
// console.log("Indici creati con successo");
})
.catch((err) => {
console.error("Errore durante la creazione degli indici:", err);
throw err; // Oppure gestisci l'errore come preferisci
});
};

View File

@@ -385,7 +385,6 @@ router.post('/updateval', authenticate, async (req, res) => {
return await CfgServer.findOneAndUpdate(
{ chiave: pair.chiave, idapp, userId: pair.userId }, { $set: pair },
{ new: false }).then((item) => {
// CfgServer.find({ chiave: pair.chiave }, (err, item) => {
if (!!item) {
res.status(200).send();
} else {