0

my contract:

user.hpp:

struct [[eosio::table]] user{//[[eosio::table]]
                // uint64_t id;
                // string username;
                // uint64_t id;            //rank 
                account_name username;        //account_name 
                uint64_t gpaplus;       // gpaplus = task_point*(((score-5)_to_gpa+1.5)/5 - 0.10) 
                string totalbounty;     //token distribution 
                int awscore;         //averageweightedscore 
                vector<int> taskpartin; // task_id 

                auto        primary_key()   const  { return username;                        }
                uint64_t    by_high_gpaplus()const { return static_cast<uint64_t>(-gpaplus) ; }
                /*************************************************
                 * static_cast<uint64_t>(-gpaplus); type convert *
                 * static_cast<uint64_t>(-1) = 2^64 -1 ***********
                 *************************************************/
            };
typedef eosio::multi_index< N(user), user,
                           indexed_by<N(gpaplus), const_mem_fun<user, uint64_t, &user::by_high_gpaplus>  >
                          >  userIndex;

user.cpp:

[[eosio::action]]
    void User::getranklist(int length){
        User::userIndex users(_self,_self);
        auto u = users.get_index<N(gpaplus)>(); // this line of code make the sort ? Do not omit.
        int i=0;
        print("{\"ranklist\": [");
        for(auto& user : u) {
            i++;
            getuser(user.username);
            if(i!=length){
                print(",");
            }else{break;}
        }
        print("]}");
    }

my frontend:

define a function:

fetchData(tableCode,tableScope,tableName){//async 
        return new Promise((resolve, reject) => {
            // const fetch = require('node-fetch');           // node only; not needed in browsers
            const rpc = new JsonRpc({
                chainId: "5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191", // 32 byte (64 char) hex string
                keyProvider: [], // WIF string or array of keys..
                httpEndpoint: 'https://api-kylin.eoslaomao.com',
                expireInSeconds: 60,
                broadcast: true,
                verbose: false, // API activity
                sign: true
            });//, { fetch } //process.env.REACT_APP_EOSIO_HTTP_URL
            // http://api-kylin.eoslaomao.com

            // const resp = 
            rpc.getTableRows({//await 
                json: true,             // Get the response as json
                code: tableCode,        // Contract that we target   eosio.token 'zjubcatask11'
                scope: tableScope,      // Account that owns the data   testacc  'zjubcatask11'
                table: tableName        // Table name  accounts in ABI file  'task' 
            }).then((resp)=>{
                // console.log("rows",resp.rows);
                resolve(resp.rows);
            }).catch(e=>{
                reject(e);
                if(window.debug)alert("getTableRows error, ", e.message);//JSON.stringify(e));
            });
        });
    }

call it :

fetchData('zjubcauser11','zjubcauser11','user').then(rowsdata=>{
            console.log("users :",rowsdata);
            this.setState({
              users: rowsdata, //jsonData.tasks
              loading: false
            });
            let myRank = -1;
            const userName = sessionStorage.getItem("userName");
            // const userName = window.loginAccount.name;
            let myGPAPlus = this.state.users.find(function(item,index){
                if(item.username === userName){
                    myRank = index+1;
                    return true;}
            });
            if(myGPAPlus){
              myGPAPlus=myGPAPlus.gpaplus;
            }else{
              myGPAPlus=0;
            }
            this.setState({
                myInfo : {myRank:myRank, myGPAPlus:myGPAPlus}
            });
          });

then the table rows data returned is not sorted...

P.S. In my local test node, the print output from contract indicates that it is sorted. But when I deploy my contract on main net, when use getTableRows to fetch data, it is not sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.