Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

Why does my variable return a different value than what the getter evaluates? [closed]

$
0
0

I have the following code:

Main:

async method() {        const values: RequestForHelp[] = await databaseHelper.getUnacceptedAndUnsucceededRequests();        for (const request of values) {            request.userHelpMeLatitude = 19;            request._userHelpMeLatitude = 20;            console.log(request.userHelpMeLatitude);            console.log(request._userHelpMeLatitude);        }    }

Why does the first console.log return 19, and the second one 20?

If I print console.log(request); I get the following output:

{"_userHelpMeLatitude": 20, ", "userHelpMeLatitude": 19}

Both the getter and setter methods are setting the "_userHelpMeLatitude" variable, so it should change its value after being set, but this is not happening and I don't know why. As a result, the value returned by the getter and the value printed out by my variable are giving different values, even though they shouldn't.

RequestForHelp:

class RequestForHelp{public _userHelpMeLatitude: number |string; public get userHelpMeLatitude(): string | number {    return this._userHelpMeLatitude;    }public set userHelpMeLatitude(value: number | string) {    this._userHelpMeLatitude = value;}}export {RequestForHelp};

DatabaseHelper:

public async getUnacceptedAndUnsucceededRequests(): Promise<RequestForHelp[]>{    const requestsForHelpRef = this.database.ref('RequestsForHelp');    const snapshot = await requestsForHelpRef.once('value');    const requestsForHelpTemp = snapshot.val();    const filteredRequestsForHelp = Object.values(requestsForHelpTemp).filter((request:any) => !request.accepted && !request.succeeded);    return filteredRequestsForHelp as RequestForHelp[];  }

Thank you in advance


Viewing all articles
Browse latest Browse all 6290

Trending Articles