mjking
5 سال پیش توسط mjking مطرح شد
0 پاسخ

sweetalert2

سلام دوستان من میخوام از sweetalert برای حذف یک دیتا استفاده کنم که وقتی روی دکمه حذف زدم ازم بپرسه آیا مطمئن به حذف این دیتا هستید یا نه؟
البته خودم این کد رو نوشتم ولی کار نمیکنه ممنون میشم کمکم کنید

const Swal = require('sweetalert2')

async destroy(req , res , next) {
        try {
            let ads = await Ads.findById(req.params.id);
            Swal.fire({
                title:'دقت کنید',
                text:'آیا مطمئن هستید از حذف تبلیغ!',
                icon: '<%= data.icon %>',
                showConfirmButton: true,
                showCancelButton: true,
                cancelButtonText: 'خیر',
                confirmButtonText: 'بله',
            }).then(result => {
                if(result.value){

                    if(!ads){
                        console.log('چنین تبلیغی یافت نشد');
                    }

                    Object.values(ads.images).forEach(image => fs.unlinkSync(`./public${image}`));

                    ads.remove();

                    return res.redirect('/dashborad/ads/my-ads');
                }else if (result.dismiss === Swal.DismissReason.cancel) {
                    return res.redirect('/dashborad/ads/my-ads');
                }
            })
        } catch (err) {
            next(err)
        }
    }

که بعد با این ارور مواحه میشم

Cannot read property 'then' of undefined

TypeError: Cannot read property 'then' of undefined
    at SweetAlert.then (C:\Users\mjking\Desktop\nodejs_cms\node_modules\sweetalert2\dist\sweetalert2.all.js:3590:24)
    at adsController.destroy (C:\Users\mjking\Desktop\nodejs_cms\app\http\controllers\dashborad\adsController.js:146:16)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)