FindDocumentByKeyValue, Debug logs.

0 votes

Here is function I'm calling to save:



public void SaveOrUpdateCoins(int coinsCount)
    {
        Dictionary<string, object> jsonDoc = new Dictionary<string, object>();
        jsonDoc.Add("Coins", coinsCount);
        storageService.SaveOrUpdateDocumentByKeyValue(GameVars.dataDB, GameVars.collectionName, OnAuthenticate.username, "Coins", jsonDoc, new OnCoinsSaved());     
    }

public class OnCoinsSaved : App42CallBack
    {
        public void OnSuccess(object response)
        {
            Storage storage = (Storage)response;
            App42Log.Console("dbName is " + storage.GetDbName());
            App42Log.Console("collection Name is " + storage.GetCollectionName());
            IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList();
            for (int i = 0; i < jsonDocList.Count; i++)
            {
                App42Log.Console("objectId is " + jsonDocList[i].GetDocId());
                App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());
                App42Log.Console("CreatedAt is : " + jsonDocList[i].GetCreatedAt());
                App42Log.Console("UpdatedAt is : " + jsonDocList[i].GetUpdatedAt());
            }
        }

        public void OnException(Exception e)
        {
            App42Log.Console("Exception : " + e);
        }
    }


Here is debug:
http://txt.do/wrma

Here is function I call to get data:
 


public void LoadCoinsCount()
    {
        storageService.FindDocumentByKeyValue(GameVars.dataDB, GameVars.collectionName, OnAuthenticate.username, "Coins", new OnCoinsLoad());   
    }


    public class OnCoinsLoad : App42CallBack
    {
        public void OnSuccess(object response)
        {
            Storage storage = (Storage)response;
            IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList();
            for (int i = 0; i < jsonDocList.Count; i++)
            {
                App42Log.Console("objectId is " + jsonDocList[i].GetDocId());
                App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());
            }
        }

        public void OnException(Exception e)
        {
            App42Log.Console("Exception : " + e);
        }
    }


Here is Debug:
http://txt.do/wrmg

 

related to an answer for: How to use "FindDocumentByKeyValue" ?
asked Jun 17, 2015 in App42 Cloud API-BaaS by nbg_yalta (10 points)

1 Answer

0 votes

Hello,

 

Thanks for sharing your logs with us. To save and update your existing document in saveORUpdateDocumentByKeyValue method, you need to send the correct key/value in the method code as shown below:

 storageService.SaveOrUpdateDocumentByKeyValue(GameVars.dataDB, GameVars.collectionName, "_$owner.owner",OnAuthenticate.username, jsonDoc, new OnCoinsSaved());     
    }
 
public class OnCoinsSaved : App42CallBack
    {
        public void OnSuccess(object response)
        {
            Storage storage = (Storage)response;
            App42Log.Console("dbName is " + storage.GetDbName());
            App42Log.Console("collection Name is " + storage.GetCollectionName());
            IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList();
            for (int i = 0; i < jsonDocList.Count; i++)
            {
                App42Log.Console("objectId is " + jsonDocList[i].GetDocId());
                App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());
                App42Log.Console("CreatedAt is : " + jsonDocList[i].GetCreatedAt());
                App42Log.Console("UpdatedAt is : " + jsonDocList[i].GetUpdatedAt());
            }
        }
 
        public void OnException(Exception e)
        {
            App42Log.Console("Exception : " + e);
        }
    }
 
Because in your case, your JSON document does not conatin a key who hold the value of username and same to fetch the document from Storage service, use the below line of code:
 
 
 storageService.FindDocumentByKeyValue(GameVars.dataDB, GameVars.collectionName, "_$owner.owner",OnAuthenticate.username, new OnCoinsSaved());     
    }
 
public class OnCoinsSaved : App42CallBack
    {
        public void OnSuccess(object response)
        {
            Storage storage = (Storage)response;
            App42Log.Console("dbName is " + storage.GetDbName());
            App42Log.Console("collection Name is " + storage.GetCollectionName());
            IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList();
            for (int i = 0; i < jsonDocList.Count; i++)
            {
                App42Log.Console("objectId is " + jsonDocList[i].GetDocId());
                App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());
                App42Log.Console("CreatedAt is : " + jsonDocList[i].GetCreatedAt());
                App42Log.Console("UpdatedAt is : " + jsonDocList[i].GetUpdatedAt());
            }
        }
 
        public void OnException(Exception e)
        {
            App42Log.Console("Exception : " + e);
        }
    }
 
Please check and let me know if it helps. 
 
Thanks,
Himanshu Sharma
App42 Team

 

answered Jun 18, 2015 by hs00105 (2,005 points)
This helps, thanks a lot, can you please tell me what is the best way to get my loaded data (coins) as int? This is what I have so far:

var json = JSON.Parse(jsonDocList[i].GetJsonDoc());
coinsCount = int.Parse(json["Coins"]);
Hello,

Great to hear that your previous query has been resolved.  As i am able to see that your coins field already contained an int value, then why you are parsing it on client side.

Thanks,
Himanshu Sharma
I didn't work with json before, am I able to get my int without parsing?
Yes, you will be able to get your coins without parsing it to int. Let me know if you face any issue.

Thanks,
Himanshu Sharma
Any example on how to?
Thanks.
You have already written an example in your above code:
var json = JSON.Parse(jsonDocList[i].GetJsonDoc());
coinsCount = json["Coins"];

Thanks,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...