0

I am new to Javascript, and Trying to write a function inside a function, but it always show undefine.

function csnotebook(){  
    function calculate_mw(peptide){     
        var total_mw=0;                                 
        var split_peptide = peptide.split("-");
        // Check if the blog id is found in database
        Aa.findOne({ three_letter: split_peptide[1] }, (err, aa) => {
        // Check if the id is a valid ID
            if (!aa) {
                console.log("wrong aa");
            }else{
                total_mw += aa.mw;
            } 
            return total_mw;
      });
    }
    var publicAPI = {                           
        mw: calculate_mw                
    };

    return  publicAPI; 
}
var fred = csnotebook();
var totalmw = fred.mw("Ala-Cys");
console.log(totalmw);

I assume i can find the corresponding value mw from database, but totalmw, I always get undefined for some reson, anybody know why? Thank you!!

Chaos
  • 599
  • 4
  • 7

1 Answers1

0

The inner function calculate_mw doesn't return anything, so the return value of a function is undefined unless you return something.

If you want to return the result of the Aa.findOne you should:

return Aa.findOne(...
Dekel
  • 60,707
  • 10
  • 101
  • 129