Home > Enterprise >  Using Meteor wrapAync and/or bindEnvironment when calling function in methods.js using async functio
Using Meteor wrapAync and/or bindEnvironment when calling function in methods.js using async functio

Time:09-19

My Meteor project has the DASH SDK installed as an NPM package. The goal is to call the "funds" function in methods.js from fixtures.js whenever a transaction is received. The address and amount are console logging from fixtures.js correctly, but I am receiving an error regarding bindEnvironment due to fibers. It does not console log from methods.js.

fixtures.js

import { Meteor } from 'meteor/meteor';

Meteor.startup(() => {
  const Dash = require("dash");
  const mnemonic = 'fake fake fake fake fake fake fake fake fake fake fake fake';
  const client = new Dash.Client({ network: "testnet", wallet: { mnemonic } });
  async function listenUnconfirmedTransaction() {
    const account = await client.wallet.getAccount();
    console.log(account);
    account.on('FETCHED/CONFIRMED_TRANSACTION', (data) => {
        console.dir(data);
        var amount = data.payload.transaction.outputs[0]._satoshis;
        var address = data.payload.transaction.outputs[0].script.toAddress("testnet").toString();
        console.log("Amount: "   amount   ", Address: "   address);
        if (address) {
            Meteor.call('funds', address, amount, (error) => { if (error) { console.log(error); }});
        };
    });
  };
  listenUnconfirmedTransaction();
}

methods.js

import { Meteor } from 'meteor/meteor';

Meteor.methods({
  'funds': function (address, received) {
    console.log("Address: "   address   ", Received: "   received);
  }
});

I have looked into the bindEnvironment and wrapAsync for Meteor, but I am not understanding how I can use them in this situation. Please advise

EDIT Error:

I20220918-19:58:40.577(-7)? Amount: 200000000, Address: yQKFphqFK7yWSCzxor9SthWwL9Z17u4TyJ
I20220918-19:58:40.624(-7)? error: Error syncing incoming transactions Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment. Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
I20220918-19:58:40.625(-7)?     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor.js:1188:11)
I20220918-19:58:40.625(-7)?     at Meteor.EnvironmentVariable.EVp.get (packages/meteor.js:1213:10)
I20220918-19:58:40.625(-7)?     at Server.applyAsync (packages/ddp-server/livedata_server.js:1774:64)
I20220918-19:58:40.626(-7)?     at Server.apply (packages/ddp-server/livedata_server.js:1739:26)
I20220918-19:58:40.626(-7)?     at Server.call (packages/ddp-server/livedata_server.js:1721:17)
I20220918-19:58:40.626(-7)?     at Account.<anonymous> (server/main.js:16:20)
I20220918-19:58:40.626(-7)?     at Account.emit (events.js:400:28)
I20220918-19:58:40.626(-7)?     at Account.emit (domain.js:475:12)
I20220918-19:58:40.626(-7)?     at Storage.<anonymous> (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Account/Account.js:107:72)
I20220918-19:58:40.626(-7)?     at Storage.emit (events.js:400:28)
I20220918-19:58:40.627(-7)?     at Storage.emit (domain.js:475:12)
I20220918-19:58:40.627(-7)?     at ChainStore.<anonymous> (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Storage/methods/createChainStore.js:21:14)
I20220918-19:58:40.628(-7)?     at ChainStore.emit (events.js:400:28)
I20220918-19:58:40.628(-7)?     at ChainStore.emit (domain.js:475:12)
I20220918-19:58:40.629(-7)?     at ChainStore.considerTransaction (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/ChainStore/methods/considerTransaction.js:92:10)
I20220918-19:58:40.630(-7)?     at /home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Account/methods/importTransactions.js:31:46
W20220918-19:58:40.630(-7)? (STDERR) (node:2339) UnhandledPromiseRejectionWarning: Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20220918-19:58:40.631(-7)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor.js:1188:11)
W20220918-19:58:40.632(-7)? (STDERR)     at Meteor.EnvironmentVariable.EVp.get (packages/meteor.js:1213:10)
W20220918-19:58:40.632(-7)? (STDERR)     at Server.applyAsync (packages/ddp-server/livedata_server.js:1774:64)
W20220918-19:58:40.632(-7)? (STDERR)     at Server.apply (packages/ddp-server/livedata_server.js:1739:26)
W20220918-19:58:40.633(-7)? (STDERR)     at Server.call (packages/ddp-server/livedata_server.js:1721:17)
W20220918-19:58:40.633(-7)? (STDERR)     at Account.<anonymous> (server/main.js:16:20)
W20220918-19:58:40.633(-7)? (STDERR)     at Account.emit (events.js:400:28)
W20220918-19:58:40.634(-7)? (STDERR)     at Account.emit (domain.js:475:12)
W20220918-19:58:40.634(-7)? (STDERR)     at Storage.<anonymous> (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Account/Account.js:107:72)
W20220918-19:58:40.634(-7)? (STDERR)     at Storage.emit (events.js:400:28)
W20220918-19:58:40.635(-7)? (STDERR)     at Storage.emit (domain.js:475:12)
W20220918-19:58:40.635(-7)? (STDERR)     at ChainStore.<anonymous> (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Storage/methods/createChainStore.js:21:14)
W20220918-19:58:40.635(-7)? (STDERR)     at ChainStore.emit (events.js:400:28)
W20220918-19:58:40.636(-7)? (STDERR)     at ChainStore.emit (domain.js:475:12)
W20220918-19:58:40.636(-7)? (STDERR)     at ChainStore.considerTransaction (/home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/ChainStore/methods/considerTransaction.js:92:10)
W20220918-19:58:40.637(-7)? (STDERR)     at /home/alexanderwshea/Meteor/dashreceiver/node_modules/@dashevo/wallet-lib/src/types/Account/methods/importTransactions.js:31:46
W20220918-19:58:40.637(-7)? (STDERR) (Use `node --trace-warnings ...` to show where the warning was created)
W20220918-19:58:40.637(-7)? (STDERR) (node:2339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
W20220918-19:58:40.638(-7)? (STDERR) (node:2339) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CodePudding user response:

Ok, if you run Meteor up to version 2.7.3 you need to wrap that(details further down).

2.8 is now in preparation and it makes the move from Fibers (still compatible with NodeJS 14 but not with Node 16) to async. I think async now works server side, just keep an eye on this: https://forums.meteor.com/t/eta-of-2-8-release/58720

Wrapping for Fibers. This is basically running async code as if it was synchronous.

const bound = Meteor.bindEnvironment(callback => callback())

 bound(() => {
    
  ...your code goes here.

 })


  • Related