neiya
neiya

Reputation: 3142

Function returns function instead of object

reI am makijng a script in Typescript, I would like to use the function convertUnixDate inside of another function getAllSMS belonging to the same class. I want the value of the date variable inside the getAllSMS function to be equal to the object returned by the convertUnixDate. But for now date equals an empty function instead of the object. Why ?

export class SMSManager {

filters: object;
constructor(filter: object) {
    this.filters = filter;
}

public static convertUnixDate(unixTimeStamp: number): object {
    let date = new Date(unixTimeStamp*1000);
    return {
        'day': date.getDate(),
        'month': date.getMonth(),
        'year': date.getFullYear(),
        'hour': date.getHours(),
        'minutes': date.getMinutes(),
        'seconds': date.getSeconds()
    };
}

public getAllSMS() {
    if (SMS) {
        SMS.listSMS(this.filters, function (data) {
            let contacts = {};
            for (let i = 0; i < data.length; i++) {
                if ((data[i].address).length > 7 && (data[i].address).match("[0-9]+")) {
                    let date = () => {
                        return this.convertUnixDate(data[i].date);
                    };
                    if (contacts.hasOwnProperty(data[i].address)) {
                        Object.defineProperty(contacts[data[i].address], data[i]._id, {
                            value: {
                                "body": data[i].body,
                                "date": date
                            }
                        });
                    } 
        }, function (err) {
            console.log('error list sms: ' + err);
        });
    }

}

}

Upvotes: 0

Views: 199

Answers (1)

tevemadar
tevemadar

Reputation: 13225

It does what you have written: the () => {...} thing is a function, and it gets stored in date.
You should simply write let date = SMSManager .convertUnixDate(data[i].date); instead.

Upvotes: 2

Related Questions