Question: where to import secret code from method
Closed this issue · 8 comments
In the Secret Code example on the Security section, how/where would you import the secret code (i.e., MMR)?
If this were imported in the methods module, which is loaded on the client, the import would fail.
Ah, that's a really good point. Unfortunately, ES2015 doesn't allow conditional imports, so we have to work around the standard a bit by calling require
:
let MMR;
if (Meteor.isServer) {
MMR = require('/my/mmr/module');
}
// .. now we can use it on the server, but not on the client
This would be a really good thing to add both in the app structure, and methods articles. If anyone has time to put together a PR, that would be awesome.
Ahh, okay. Thank you for the clarification.
I may put a PR together that includes this and some of the material on async/sync w/in methods. Granted, the async section of using-packages covers most of what I'd like to see for the latter.
I'd suggest making those separate PRs just to keep the discussions separate!
Similar discussion here: meteor/validated-method#40
This had me stumped to. I don't think the guide is explicit enough here w.r.t finally importing and running the secret sauce. Based on your response above I used require inside the !isSimulation block of a method definition. A few more words in the guide would have saved me a lot of head scratching.
run({ amount, currency, source, metadata }) {
if (this.isSimulation) {
console.log('client side method simulation');
} else {
// only run server-side
let charge = require('./server/charge').default();
let result = charge({ amount, currency, source, metadata });
return result
}
},
This is still not clear in the meteor guide! Please give an example of the module export and the import of MMR.