0

I have defined a javascript function savedata() to save the data and return a string according to the condition given.

I have called the function in checkavailabily() function and console logged the data. In that, it is giving me undefined.

can anyone help me with this?

function savedata(pin,dataarray)
{
    vaccinenotifier.findOne({pincode : pin},function(err,result){
        // console.log(result);
        var rlen = result.data.length;
        var dlen = dataarray.length;
        if(result.data.length === 0)
        {
            result.data = dataarray;
            result.save((error, ur) => {
                console.log(ur);
                // console.log(error);
            });
            return "newdata";
        }
        else if((rlen === dlen) && (dataarray[dlen - 1] === result.data[rlen - 1]))
        {
            return "same";
        }
        else
        {
            result.data = dataarray;
            result.save((error, ur) => {
                console.log(ur);
            });
            return "updatedata";
        }
    });
}


async function checkAvailability(pincode) {

    
    const promises = [];

    let datesArray = await fetchNext10Days();
    var i = 0;
    for(i = 0; i<datesArray.length; i++) {
        var date = datesArray[i];
        promises.push(getSlotsForDate(date, pincode));
    }

    Promise.all(promises)
        .then((data) => {
            var dataArray = new Array();
            for(var p of data)
            {
                if(p)
                {
                    // console.log(p);
                    dataArray.push(p);
                }
            }

            console.log("data array :- ", dataArray);
            console.log(pincode," : ",savedata(pincode, dataArray));
            
        })
}
  • returning a value from the callback function does not make it a return value of the outer function. – Yousaf May 23 '21 at 14:06
  • 1
    savedata doesn't return anything, you've confused it with the function you are passing findone, just return the fineone result – Keith Nicholas May 23 '21 at 14:06

0 Answers0