Cannot get the updated value in MongoDB realm

40 Views Asked by At

I have this function on the server said that updates some values for my user:

exports = async function(userId, district){
    var userCollection = context.services.get("mongodb-atlas").db("db").collection("User");

    
    try {
        var result = await userCollection.findOneAndUpdate(
            {"_id": userId},
            { $set: {district: district, adresa: district, userLocation:{ latitude: districtLatitude[district], longitude: districtLongitude[district]}}}
        )
        
        if(result._id==userId)
        {
            return true;
        }
    } catch (error) {
        console.error("Error updating user district:", error);
        return false; 
    }
};

I want to return true after the update is finished, I hope that I have done it in the right way.

For some reason, after I update, I cannot get the new value in the backend, it still get the old one, kotlin:

    coroutineScope.launch {
          val alreadyUpdated = loginViewModel.changeDistrictFunctionWithParameters(selectedCity)
    
          if(alreadyUpdated) {
                districtChoose.value = true
              }


   suspend fun changeDistrictFunctionWithParameters(district: String): Boolean {
       val user = appService.currentUser!!
       return user.functions.call("ChangeDistrictFunctionWithParameters", user.id, district)
        }

    fun getUserDistrict(): String {
            val userId = appService.currentUser?.id
    
            return realm.query<UserInfo>("_id = $0", userId).first().find()!!.district
        }

Sometimes getUserDistrict() returns the updated value, sometimes no. Its probably because of data sync. Any idea how to be sure that the query return the latest data for my user?

0

There are 0 best solutions below