About Javascript object - javascript

I am working on my react native project, and having problem on below function
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
console.log(notif.custom_notification);
console.log(notif.custom_notification.body);
});
for the console.log(notif.custom_notification); it can log
{"show_in_foreground":true,"sound":"default","title":"MyNewApp","body":"ffff","priority":"high"}
for the console.log(notif.custom_notification.body); it log
undefined
How can I can the body of the notif.custom_notification?

I think you need to parse it first, otherwise everything in your code looks good. var i = JSON.parse(notif.custom_notification); i.body

Related

CSVtoJSON returns UnhandledPromiseRejectionWarning: ReferenceError: JavaScript

I am trying to convert CSV from a CSV file to JSON.
I have the following code
(async() => {
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
let latest_provinces_confirmed = source;
});
console.log(latest_provinces_confirmed)
...
and when I run it, I get
UnhandledPromiseRejectionWarning: ReferenceError: latest_provinces_confirmed is not defined
How can I get the result of the CSVtoJSON into a variable to use later
Thanks in advance
The variable 'lastest_provinces_confirmed' is declared inside the anonymous function so you cannot access it outside that function. Thats why console.logging variable doesn't work.
You can try to pass that variable outside of that function by returning it OR you can forward declare that variable outside those functions to be able to access it.
Something like this might work:
let latest_provinces_confirmed = csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
return source;
});
Remember to take account that you are working with async functions so you have to make sure that function csvToJson() has run before using 'latest_provinces_confirmed'. You can check use of "await" in order to do that!
You are doing a simple and a beginner level mistake. The scope of "let" is only inside the block. Change your code as below and it should work.
(async() => {
let latest_provinces_confirmed;
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
latest_provinces_confirmed = source;
console.log(latest_provinces_confirmed)
....
});

How to show result of console.timeEnd() in react native android app?

I have installed package and import it like this:
import 'react-native-console-time-polyfill';
and have function like this:
search = () => {
let s = this.state.file.toLowerCase();
let p = this.state.search.toLowerCase();
console.time('t');
let result = kmp.findAll(s, p);
let time = console.timeEnd('t');
alert(time);
};
Why the result in alert returned as "undefined"?
My expectation is result time for processing function is show up at alert popup. Search is a function onpress at button.
Sorry my bad english, hopefully you guys understand my question ^^
The way the library you are using is written, it only outputs the resulting time to console, it does not return it. Because of that, while you can see it in debug console, you will always receive undefined from both .time() and .timeEnd() functions. This is also the same for browsers, and you can actually test it in your Javascript console.
However the library's code seems to be short, you can actually add the functionality. If you add return delta.toFixed(3); at the end of the .timeEnd() function (21st line in the index.js) you can get the result you want.

Looking for a way to write custom Puppeteer commands

Previously using Nightwatch.js I was able to create custom Nightwatch commands: https://github.com/nightwatchjs/nightwatch-docs/blob/master/guide/extending-nightwatch/custom-commands.md
I'm wondering if there is anything that exists like this for Puppeteer-- the closest thing I've seen is: Is there a way to add script to add new functions in evaluate() context of chrome+puppeeter?
But it's still far away from what I want. I would like to be able to call page.commonAction(...) instead of
page.x();
page.y();
page.z();
You can always create your own script with functions you can call. For example, I have a myFunctions.js in the same folder, from which I name
const mf = require('./myFunctions.js');
Inside the myFunctions.js I have, for example, this function:
async function verifyElementPresent(page, selector) {
let verifySelector = await page.$(selector);
if (verifySelector !== null) {
console.log(found('> OK - Element present'));
} else {
console.log(notfound('>>> ERROR - Element not present: ' + selector));
}
}
So now, in my Puppeteer script all I have to do is write:
mf.verifyElementPresent(page, '#headerTitle');
And it'll print in console the result.
Hope this helps.

Javascript require returns empty object

I am trying to use a library found on the web, called himalaya, which is an html parser.
https://github.com/andrejewski/himalaya
I followed their guide on importing the library, so I do
var himalaya = require('himalaya');
However when I call one of its member functions, I get an error
TypeError: himalaya.parse is not a function
I tried executing himalaya.parse() on the web browser console directly, it works. I tried commenting out the require statement in the js file, the function no longer works on web browser.
I guess this implies the require statement works? But for some reasons I cannot use it in my javascript file, only on the browser console.
Perhaps something with file scopes? Here is part of my code.
var himalaya = require('himalaya');
Template.main.onCreated(function () {
var http = new HttpGet("www.someurl.com/", "/somedirectories/", function (response) {
console.log(himalaya.parse(response.content));
});
http.sendRequest();
});
I am certain that response.content does contain a valid html string.
When you call the himalaya.parse inside the main.onCreated function it seems like the library is not completed loaded at that time. That's why it only runs in your browser console. Check if the himalaya library has a onReady function to let you know exactly when you can use it. If not, you can:
a) Call the parse function inside the main.onRendered or
b) Keep the parse call inside the main.onCreated and set a timeOut to call it after a half second like this:
var himalaya = require('himalaya');
Template.main.onCreated(function () {
var http = new HttpGet("www.someurl.com/", "/somedirectories/", function (response) {
setTimeout(function(){himalaya.parse(response.content)},500);
});
http.sendRequest();
});
If you have an issue with the setTimeout check this answer:
Meteor.setTimeout function doesn't work

Parse Query find method returns object not array

I am working with a mobile App which uses Parse as a backend and I have an issue with the find function. When running the find function in the format of:
var = firstQuery = (new Parse.Query("MyParseObject"))
.find(),
secondQuery = (new Parse.Query("OtherParseObject")).get(id)
// there is only one object that firstQuery can find
Parse.Promise.when(firstQuery, secondQuery)
.then( function (query1res, query2res) {
// query1res should return only one result wrapped in an array,
// instead query1res is an object without a get method
query1res.forEach (function (res) {
// this fails: cannot get .length of undefined
})
// ... do stuff with the returned data
})
Is there something i am missing? I am sure this used to work before, but now it does not.
It is quite difficult to correctly get in an debug this issue thanks to the way Parse works, but their docs outline that this should return an array, but it does not at this point of time.
Thanks for your help.
Based on the Parse docs, it looks like Parse.Promise.when expects an array, although based on this support thread, the results will be passed in as individual arguments to the then handler. Give this a try:
Parse.Promise.when([firstQuery, secondQuery])
.then(function (query1res, query2res) {
// use query1res and query2res
});
Turns out it was down to a function deeper in the code, which needed to return a promise to chain off of. after adding this the code was happy enough. The function that needed to return the promise was called in the forEach and has nothing to do with the initial two queries, which is what was throwing me.

Categories

Resources