Receiving 66:285 Uncaught TypeError: Cannot read property 'then' of undefined

Viewed 350

I have designed a simple ajax request for deleting a file from database in mvc. for that i am using javascritp ajax with swal prompt for delete. But it is not working. I am getting .then undefined error.

Here is the code--

 $(".btnDel").click(function () {

    var NewFileName = $(this).val();
    var id =@Model.Pd.Id;
    console.log(id);
    console.log(NewFileName);

    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        icon: "warning",
        
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
    })

    .then(function (isConfirm) {
        if (isConfirm) {
             $.ajax({
                type: "POST",
                url: "@Url.Content("~/ManageProducts/DeleteExistingFile")/",
                data: { 'id': id, 'fileName': NewFileName },
                success: function (data) {
                    swal("Message.", data, "success");
                    location.reload();

                },
                error: function () {
                    swal("", "Something went wrong", "error");
                }
             });
           
        }
        else
        {
         swal("Cancelled", "Your imaginary file is safe :)", "error");

        }
    });
});

enter image description here

enter image description here

1 Answers

Thanks for the edit. The snippet below defines id and NewFileName for testing and uses example.com for the POST - if you try to delete the file you get the "oops" message. In other respects it should be the same as the code in the post.

const id = "example_id";
const NewFileName = "example.pdf";
//**********************************

   swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        icon: "warning",
        
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
    })

    .then(function (isConfirm) {
        if (isConfirm) {
             $.ajax({
                type: "POST",
                url: "https:www.example.com/delete",
                data: { 'id': id, 'fileName': NewFileName },
                success: function (data) {
                    swal("Message.", data, "success");
                    location.reload();

                },
                error: function () {
                    swal("", "Something went wrong", "error");
                }
             });
           
        }
        else
        {
         swal("Cancelled", "Your imaginary file is safe :)", "error");

        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

I am at a loss to explain why the snippet works but running it in VS says swal returns undefined. I would test the code in a browser first to see if it still errors.

Disclaimer: this is not the answer. . . yet. I am happy to delete it if anyone solves the mystery.

Related