Storing json data

0 votes

Thanks, Himanshu but I tried setting the sessionId on the storageService and still can't get data to write. Here are two functions I've been using. The first can read the data, the second can't write it.

 


--this function works fine
fetchVerifiedUserData = function(mailHash)
    local dbName = "server"
    local collectionName = "users"
    local key = "email"
    local value = mailHash
    local jsonDoc = nil

    local App42CallBackForFetch = {}
    function App42CallBackForFetch:onSuccess(object)
        print ("Got user data") 
        print("dbName is "..object:getDbName())  
        for i=1,table.getn(object:getJsonDocList()) do  
            print("DocId is "..object:getJsonDocList()[i]:getDocId())  
            print("CreatedAt is "..object:getJsonDocList()[i]:getCreatedAt())  
        end  
        jsonDoc = object.jsonDocList[1].jsonDoc
      
        for k, v in pairs(jsonDoc) do
            Global.user[k] = v
            print ("k/v", k,v)
        end

        if Global.user["verified"] == "false" then
            print ("set true locally")
            Global.user["verified"] = "true"
        end

    end
    function App42CallBackForFetch:onException(exception)
        print("Message is : "..exception:getMessage())  
        print("App Error code is : "..exception:getAppErrorCode())  
        print("Http Error code is "..exception:getHttpErrorCode())  
        print("Detail is : "..exception:getDetails())

    end
    --add check to see if exists
    storageService:findDocumentByKeyValue(dbName, collectionName, key, value, App42CallBackForFetch)
end

--this function fails
writeDataToDB = function (mailHash, dataTable)
    local dbName = "server"
    local collectionName = "users"
    local key = "email"
    local value = mailHash --mailHash
    local jsonDoc = {}
    jsonDoc.email = true

    local App42CallBackForWrite = {}

    function App42CallBackForWrite:onSuccess(object)
        print ("Wrote data") 
        print("dbName is "..object:getDbName())  
        for i=1,table.getn(object:getJsonDocList()) do  
            print("DocId is "..object:getJsonDocList()[i]:getDocId())  
            print("CreatedAt is "..object:getJsonDocList()[i]:getCreatedAt())  
        end

    end
    function App42CallBackForWrite:onException(exception)
        print ("Failed to write data")
        print("Message is : "..exception:getMessage())  
        print("App Error code is : "..exception:getAppErrorCode())  
        print("Http Error code is "..exception:getHttpErrorCode())  
        print("Detail is : "..exception:getDetails())

    end
    
    storageService:updateDocumentByKeyValue(dbName,collectionName,key,value,jsonDoc,App42CallBackForWrite) 
end

--- error info
Header Key is : dataACL   Header Value is : ""
                    Header Key is : emailAuth   Header Value is : "true"
                    Header Key is : jsonObject   Header Value is : "[]"
                    Header Key is : loggedInUser   Header Value is : "Tom"
                    Header Key is : resource   Header Value is : "storage"
                    Header Key is : selectKeys   Header Value is : "[]"
                    Header Key is : sessionId   Header Value is : "eb8f3d83-d8f4-474a-a90f-c120db74b7c1"
                    Header Key is : signature   Header Value is : "YjHSCFGkpj5o43XSFRzRaQx7ppw%3D"
                    Header Key is : timeStamp   Header Value is : "2016-10-07T04:31:36.000Z"
                    Header Key is : version   Header Value is : "1.0"
                    Requested URL is : https://api.shephertz.com/cloud/1.0/storage/update/dbName/Server/collectionName/users/email/c876f512363ed428a6b31cfee9432f18?
Oct 06 09:31:36.312 Excpetion is : {"app42Fault":{"httpErrorCode":404,"appErrorCode":2601,"message":"Not Found","details":"Document by key 'email' and value 'c876f512363ed428a6b31cfee9432f18' does not exist."}}
Oct 06 09:31:36.312 No
                    Message is : Not Found
                    App Error code is : 2601
                    Http Error code is 404
                    Detail is : Document by key 'email' and value 'c876f512363ed428a6b31cfee9432f18' does not exist.

 

related to an answer for: Setting user data via Corona
asked Oct 7, 2016 in Corona by tc (10 points)

1 Answer

0 votes

Hi TC,

Thanks for sharing the code. Can you check the document in AppHQ Management Console that which user have write access to update that document? It seems like the document you are trying to update have Public Read access but not have write access. So please check if this is the case, then you have to provide the permission first to Tom then he will be able to update that document. 

Please check and let me know if it helps. 

Regards,

Rajeev

answered Oct 7, 2016 by rajeev.etc (1,660 points)
setting data with corona, continued
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
...