0

I'm trying to get a response from a function, to execute another function, but I don't receive the response. I get the following error on: "TypeError: Cannot read property 'then' of undefined." I followed this tutorial, I tried to use Promises but no success. Here's my code

function i_emp() {
    const form = $("#msform");
    const formdata = getFormData(form); // Return a data inputs JSON
    const table_emp = formdata.table_emp;
    const json_emp = {
        // DATA HERE
    };
    $.ajax({
        url: "./class/Class_Insert.php",
        type: "POST",
        dataType: "json",
        data: {
            table_name: table_emp,
            data: json_emp,
        },
        success: function(result) {
            console.log(result);
        },
        error: function() {
            console.log({
                error: "Error"
            });
        }
    }).then(() => {
        return json_emp;
    });

    function env() {

        i_emp().then(function(result) {
            console.log(result);
        });

        // const resultado = i_emp();  // I tryed this one but no success
        // console.log(resultado); 

    }
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • Your JS won't compile because its are missing. You should provide a [mcve] and ensure it replicates the problem. – Quentin May 18 '21 at 13:14
  • 2
    Your function `i_emp()` doesn't seem to be returning anything. Hence, `i_emp().then()` means you're doing `undefined.then()` – Yannick K May 18 '21 at 13:15
  • return new Promise -> resolve with data – Sagar V May 18 '21 at 13:16
  • even though you are returning `json_emp` from your `then`, it is not a return for `i_emp()` but for the closure function of `then`, you should instead call `env` and add a parameter called `result` where `env(json_emp)` is called from within your `then` function – AGE May 18 '21 at 13:16
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – BraveButter May 18 '21 at 13:16

2 Answers2

1
function i_emp() {
  return new Promise(function(resolve, reject){
    const form = $("#msform");
    const formdata = getFormData(form); // Return a data inputs JSON
    const table_emp = formdata.table_emp;
    const json_emp = {
        // DATA HERE
    };
    $.ajax({
        url: "./class/Class_Insert.php",
        type: "POST",
        dataType: "json",
        data: {
            table_name: table_emp,
            data: json_emp,
        },
        success: function(result) {
            resolve(result);
        },
        error: function() {
            reject({
                error: "Error"
            });
        }
    });
 });
}

function env(){
 i_emp().then(function(result) {
  console.log(result);
 }, function(error){
  console.log(error);
 });
}
Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52
0

.then is called on ajax call but ajax is not a promise, That's why you are getting error. Either you can create a promise over the ajax call or you can return json_emp on success function in ajax

Akhil M
  • 544
  • 6
  • 8