near / near-cli

General purpose command line tools for interacting with NEAR Protocol
https://docs.near.org
MIT License
193 stars 91 forks source link

Error: [-32700] Parse error: Failed parsing args: missing field `account_id` in my dAapp frontend #1038

Closed Zaniyar closed 6 months ago

Zaniyar commented 1 year ago

I get this error (not on the near CLI - the login works there) in the frontend when I try to call a method on the contract, "Failed parsing args: missing field account_id" but the account_id is there when I log it:

User is logged in; wallet.accountId is existing;

    myContract.nftSupply(wallet.accountId)
      .then(success)
      .catch(x=>{console.log("supply error",x)})

  console.log("my args",args)

Output:

my args: Object: account_id: "blabla.testnet"[[Prototype]]: Object

App.js:100 supply error Error: [-32700] Parse error: Failed parsing args: missing field account_id at json-rpc-provider.js:329:31 at async parcelRequire.register.e.exports.default (exponential-backoff.js:7:15) at async f.sendJsonRpc (json-rpc-provider.js:304:15) at async f.query (json-rpc-provider.js:116:13) at async $3875d6d9a9da9cc6$export$bcca3ea514774656.viewMethod (near-wallet.js:70:26) at async $2081698ec9a6d70e$export$eabe01de1e942655.nftSupply (near-interface.js:3:14)

I saw similar errors related to the CLI login function, but on my side that works, but not the frontend.

romanzac commented 1 year ago

I have similar problem:

json-rpc-provider.js:366 Uncaught (in promise) Error: [-32700] Parse error: Failed parsing args: missing field account_id at l. (json-rpc-provider.js:366:35) at Generator.next () at o (json-rpc-provider.js:5:58)

What I found so far the near-wallet.js module has undefined "contractId" when calling

async viewMethod({ contractId, method, args = {} }) { const { network } = this.walletSelector.options; const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });

    console.log(contractId)

    let res = await provider.query({
        request_type: 'call_function',
        account_id: contractId,
        method_name: method,
        args_base64: Buffer.from(JSON.stringify(args)).toString('base64'),
        finality: 'optimistic',
    });
    return JSON.parse(Buffer.from(res.result).toString());
}

Hardcoding the contractId has solved the problem temporarily.

Source code: https://github.com/romanzac/sticky-habits/blob/main/frontend/near-wallet.js
romanzac commented 1 year ago

Problem solved for myself. Parcel was not passing environment variables so my code could read process.env.CONTRACT_NAME. What helped was to remove "process" "devDependencies": {}, delete "node_modules" directory and let parcel to install for Node builtin module "process" during "npm run build".

parcel build index.html --public-url ./

Installing process...

@parcel/resolver-default: Auto installing polyfill for Node builtin module "process"...

/Users/roman/sources/sticky-habits/frontend/node_modules/js-sha256/src/sha256.js:20:54 19 | var WEB_WORKER = !WINDOW && typeof self === 'object';

20 | var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; | ^^^^^^^ used here 21 | if (NODE_JS) { 22 | root = global;

https://en.parceljs.org/env.html https://parceljs.org/features/node-emulation/#environment-variables https://parceljs.org/features/node-emulation/#polyfilling-%26-excluding-builtin-node-modules

gagdiez commented 6 months ago

this problem is unrelated to near-cli, please open it back if necessary in the right repositories, or reach out dev support through telegram or discord