Using Meteor.wrapAsync correctly - javascript

Hopefully this is a newbie question.
I have the following code that I am trying to convert to using meteor.wrapAsync. I am getting a "Exception while invoking method 'emailSend' ReferenceError: syncfunc is not defined" exception. What am i missing?
Stack Trace:
I20191031-06:21:16.246(-5)? Exception while invoking method 'emailSend' ReferenceError: syncfunc is not defined
I20191031-06:21:16.248(-5)? at MethodInvocation.emailSend (src/imports/api/email.js:13:27)
I20191031-06:21:16.249(-5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20191031-06:21:16.273(-5)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20191031-06:21:16.275(-5)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1234:12)
I20191031-06:21:16.276(-5)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20191031-06:21:16.277(-5)? at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1234:12)
I20191031-06:21:16.277(-5)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20191031-06:21:16.278(-5)? at new Promise (<anonymous>)
I20191031-06:21:16.279(-5)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20191031-06:21:16.280(-5)? at packages/ddp-server/livedata_server.js:559:43
email.js:
Meteor.methods(
{
emailSend(fromAddress, subject, emailText)
{
if (Meteor.isServer)
{
const { Email } = require('../server/email.js');
var syncFunc = Meteor.wrapAsync(Email.send);
var sendEmailReturn=syncfunc(fromAddress, subject, emailText);
return sendEmailReturn;
**//if I comment out the above three lines and uncomment the line below then the application works fine.**
//return Email.send(fromAddress, subject, emailText);
}
},
})

You don't need to use external callback to sync methods as Meteor supports "async" and "awaits" by default. Below is an example of using 'await' method.
Meteor.methods({
async emailSend(fromAddress, subject, emailText) {
const { Email } = require('../server/email.js');
var sendEmailReturn = await Email.send(fromAddress, subject, emailText);
}
});

I believe Meteor.defer is more suited to what you're trying to achieve here.
Example:
Meteor.methods({
'action_plus_email': function () {
// do something
Meteor.defer(() => {
Email.send(...)
})
return 'hello there, user';
}
})
https://www.meteor-tuts.com/chapters/1/meteorsnacks#Meteor-defer
https://apiko.com/blog/organization-of-email-sending-in-meteorjs/
And if you're are going to be sending many emails, please take a look at mail-time. It can be of great help.
https://github.com/VeliovGroup/Mail-Time

Related

Problems using promiseToFuture with an object coming from context.callmethod ()

As the title suggests, I am trying to use promiseToFuture (function of the dart library dart:js_util) on an object deriving from the method of DART:JS which is context.callmethod (''), whose objective is to perform a JavaScript function, which identifies through the string passed as a parameter; The code below:
void onWalletConnect() {
var promiseWallet =
await promiseToFuture(js.context.callMethod('JsConnectWallet'));
// ignore: avoid_print
print(promiseWallet);
if (promiseWallet['address'].compareTo('errorAddress') != 0) {
setState(() {
walletAddress = promiseWallet['address'];
isWalletconnect = true;
});
}
}
// "`js.context.callMethod('..')` Returns an Object Promise; "
Below the import:
import 'dart:js' as js;
import 'dart:js_util';
To the execution of promisetofutures() [and I am sure that this is the function that generates error and not the js.context.callMethod()] I get the following error from the Chrome console:
"
Uncaught (in promise) TypeError: js.Promise.then in not a function
at Object.promiseToFuture (js_util.dart:275:35)
at _first._MyHomePageState.new.OnWalletConnect (_first.dart:28:1)
"
I don't understand why you generate this error and how to solve it, can anyone help me?

Calling Firebase Function from Unity throws error

I am having problems with my Unity3D calling Firebase Functions function. My code is actually copied from https://firebase.google.com/docs/functions/callable.
My function code is following: (just copied this file actually)
https://github.com/firebase/quickstart-js/blob/a579893cfa33121952aeed9069c1554ed4e65b7e/functions/functions/index.js#L44-L50
and in Unity I have this:
//Create the arguments to the callable function.
var data = new Dictionary<string, object>();
data["text"] = "message";
data["push"] = true;
//Call the function and extract the operation from the result.
var function = FirebaseFunctions.DefaultInstance.GetHttpsCallable("addMessage");
function.CallAsync(data).ContinueWith((task) => {
if (task.IsFaulted)
{
foreach (var inner in task.Exception.InnerExceptions)
{
if (inner is FunctionsException)
{
Debug.Log(inner.Message);
}
}
}
else
{
Debug.Log("Finished: " + task.Result.Data);
}
});
But I am getting this result:
Response is not valid JSON object.
What am I doing wrong?
Thank you for your help!!!
I was still working on that problem and suddenly it worked. I dont know why to be honest, because the could looks exactely the same and I did not change anything on that.

Selenium-cucumber.js test failed to run while adding a BeforeScenario in hooks.js file

Framework using: selenium-cucumber-js.
I am trying to run the below selenium-cucumber-js test. I would like to run the loginApp() function as BeforeScenario written in hooks.js file. But while running the test, its throwing below error at the moment. Could someone please advise on how to resolve the problem.
`C:\Tests\cucumber\node_modules\cucumber\lib\cucumber\runtime\event_broadcaster.js:30 process.nextTick(function(){ throw error; }); // prevent swallow by unhandled rejection
TypeError: node_modules\cucumber\lib\cucumber\support_code\library.js:17 scenario.loginApp is not a function
at C:\Tests\cucumber\step-definitions\hooks.js:4:51
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:390:7)
at startup (bootstrap_node.js:150:9)`
/**/Feature:**
//cucumber/features
Feature: Login and look for the Register tab
Scenario: Check the register tab in application
When After login look for "Register" in navbar
//**lookfortab.js**
//cucumber/step-definitions
const expect = require('chai').expect;
module.exports = function() {
this.When(/^After login look for "([^"]*)" in navbar$/, function (registerText) {
let navText = By.css('div#nav>div>ul>li>a');
driver.wait(until.elementLocated(navText, 10000));
return driver.findElement(navText).getText().then(el => {
console.log("print text here:"+el);
const displayTxt = el;
expect(displayTxt).to.be.eql(registerText);
});
})
}
//login.js
//cucumber/page-objects
module.exports = {
loginApp(){
this.driver.helpers.loadPage('https://testingsite.com')
this.driver.findElement(by.id('HomeLogin_Username')).sendKeys("Tester");
this.driver.findElement(by.id('HomeLogin_Password')).sendKeys("SomePassword123");
let lgBtn = By.css('div#login-fields>div>button');
this.driver.findElement(lgBtn).click();
}
};
//hooks.js
//cucumber/step-definitions
module.exports = function () {
this.BeforeScenario(function(scenario, done) {
console.log('BeforeScenario: ' + scenario.loginApp());
done();
});
};
First of all, note that selenium-cucumber-js is using a rather old version of cucumber-js (1.3.3 vs 5.0.2 at the moment of writing this). Keep that in mind when you're consulting cucumber-js docs or looking for examples. Below, I'll be posting links to the cucumber-js#1.3.3 docs.
As for what's not working in your set up:
scenario object that is passed into your hooks doesn't have page objects attached to it. It only contains some meta info about the scenario and methods to get it. Here's a list of them: cucumber-js docs
If you want to reference the page objects from your hooks, you can use the global page as described here: Page objects - selenium-cucumber-js
Your loginApp() function probably still won't work because you're accessing webdriver as this.driver and helpers as this.driver.helpers. They're also globally defined by selenium-cucumber-js: Helpers
So, your hooks.js should look like this:
module.exports = function () {
this.BeforeScenario(function(scenario, done) {
console.log('BeforeScenario: ' + page.login.loginApp());
done();
});
};
And your login.js:
module.exports = {
loginApp(){
helpers.loadPage('https://example.com')
driver.findElement(by.id('HomeLogin_Username')).sendKeys("Tester");
driver.findElement(by.id('HomeLogin_Password')).sendKeys("SomePassword123");
let lgBtn = By.css('div#login-fields>div>button');
driver.findElement(lgBtn).click();
}
};

How to report console.error with Sentry?

I have application where some critical issues are reported with console.error but are not thrown so application might continue to run - possibly in crippled state.
It's necessary to report also console.error issues, but Sentry (Raven) library send to server only thrown exceptions.
Does someone knows how to solve this nicely ?
(ideally without need to rewrite all console.error calls, cause also some vendor libraries might still write output just into console)
As user #kumar303 mentioned in his comment to the question ... you can use the JS console integration Sentry.Integrations.CaptureConsole.
See https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole for documentation.
At the end you JS code to setup Sentry looks as follows:
import * as Sentry from '#sentry/browser';
import { CaptureConsole } from '#sentry/integrations';
Sentry.init({
dsn: 'https://your-sentry-server-dsn',
integrations: [
new CaptureConsole({
levels: ['error']
})
],
release: '1.0.0',
environment: 'prod',
maxBreadcrumbs: 50
})
If then someone calls console.error a new event will sent to sentry.
Here's a more robust override solution
// creating function declarations for better stacktraces (otherwise they'd be anonymous function expressions)
var oldConsoleError = console.error;
console.error = reportingConsoleError; // defined via function hoisting
function reportingConsoleError() {
var args = Array.prototype.slice.call(arguments);
Sentry.captureException(reduceConsoleArgs(args), { level: 'error' });
return oldConsoleError.apply(console, args);
};
var oldConsoleWarn = console.warn;
console.warn = reportingConsoleWarn; // defined via function hoisting
function reportingConsoleWarn() {
var args = Array.prototype.slice.call(arguments);
Sentry.captureMessage(reduceConsoleArgs(args), { level: 'warning' });
return oldConsoleWarn.apply(console, args);
}
function reduceConsoleArgs(args) {
let errorMsg = args[0];
// Make sure errorMsg is either an error or string.
// It's therefore best to pass in new Error('msg') instead of just 'msg' since
// that'll give you a stack trace leading up to the creation of that new Error
// whereas if you just pass in a plain string 'msg', the stack trace will include
// reportingConsoleError and reportingConsoleCall
if (!(errorMsg instanceof Error)) {
// stringify all args as a new Error (which creates a stack trace)
errorMsg = new Error(
args.reduce(function(accumulator, currentValue) {
return accumulator.toString() + ' ' + currentValue.toString();
}, '')
);
}
return errorMsg;
}
Based on #Marc Schmid's solution I came up with the following working example, if you link to the Sentry CDN files.
<script src="https://browser.sentry-cdn.com/5.11.1/bundle.min.js" integrity="sha384-r7/ZcDRYpWjCNXLUKk3iuyyyEcDJ+o+3M5CqXP5GUGODYbolXewNHAZLYSJ3ZHcV" crossorigin="anonymous"></script>
<!-- https://github.com/getsentry/sentry-javascript/issues/1976#issuecomment-492260648 -->
<script src="https://browser.sentry-cdn.com/5.11.1/captureconsole.min.js"></script>
<script>
Sentry.init({
dsn: 'https://abcdef1234567890#sentry.io/012345',
debug: false,
integrations: [
new Sentry.Integrations.CaptureConsole({
levels: ['error']
})
],
});
</script>
Found a little hacky solution:
const consoleError = console.error;
console.error = function(firstParam) {
const response = consoleError.apply(console, arguments);
Raven.captureException(firstParam, { level: 'error' });
return response;
};
It just wraps console.error and report each of error logs in console to Raven (Sentry).
If someone have nicer approach (maybe some hidden feature of Sentry) please feel free to share!
I wrote a library that is going this using your Sentry instance.
https://github.com/aneldev/dyna-sentry

NodeJS gRPC: "Method handler expected but not provided"

I plowed through the docs and haven't found a solution yet. The app is loosely based on the "sayHello"-example from their docs but every time the code runs the warning Method handler for /eventComm.DatabaseRPC/InsertSingleDocument expected but not provided is returned.
My proto file:
service DatabaseRPC {
rpc InsertSingleDocument (Doc) returns (Doc) {}
}
message Doc {
required string name = 1;
required int32 id = 2;
}
My gRPC Server:
function InsertSingleDocument (call, callback) {
callback(null, {
name: 'Hello ',
id: 1
})
}
let server = new grpc.Server()
server.addProtoService(protoDef.DatabaseRPC.service, {
InsertSingleDocument: InsertSingleDocument
})
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
What is the problem with this code? Of course I already tried to google the error but found no solution
To conform with JavaScript naming conventions, methods should be provided with the first letter lowercased:
server.addProtoService(protoDef.DatabaseRPC.service, {
insertSingleDocument: InsertSingleDocument
})
You can see this in the Hello World example you linked. The method is declared as SayHello in the proto file, but is passed to the server as sayHello.
Note: I agree that this is confusing, and I will try to improve the situation.

Categories

Resources