learnNcode
learnNcode

Reputation: 149

TypeError: Cannot read property 'then' of undefined --got this error while using then

I tried the below code since node is async type inorder to run my code sequentialy I added then function, but I got the error TypeError: Cannot read property 'then' of undefined. I used the below code

    va

r arr =[]
    exceltojson({
       input: 'test.xlsx',
       output: 'test.json',// Don't need output
       sheet: 'test'
     },
     function(err, result) {
       if (err) {
         console.error(err);
       } 
    }
    )
    .then() {
      // var msg ='done';
       updateFlag();
       })

    function updateFlag(){
       console.log('end')
    }
    I expect the output like 

conversion done

    end

Upvotes: 0

Views: 637

Answers (1)

amit gupta
amit gupta

Reputation: 866

you function exceltojson is not a returning a promise and this is the problem. I have converted your function into a promise. Please try the code given below.

function promiseExcelToJson() {
return new Promise((res, rej) => {
    exceltojson({
        input: 'test.xlsx',
        output: 'test.json',
        sheet: 'test'
      },
      function(err, result) {
         if(!err) { 
         console.log('conversion done') 
         res(result) 
        } else 
          rej(result)
      }

    )
})
}

var arr =[]
promiseExcelToJson().then(()=> {
    updateFlag();
})
function updateFlag(){
console.log('end')
}

Hope this helps! Let me know!

Upvotes: 2

Related Questions