api examples have bad callback syntax for safari and mobile chrome - bigcommerce

So in this example, the callback function syntax will break on Safari and mobile Chrome and Safari.
utils.api.cart.getContent(options, (err, response) => {
$modalOverlay.hide();
$cartCounter.text(response.data.cart.items.length + " items in your cart");
$modalContent.html(response.content);
});
Should be written as ...
utils.api.cart.getContent(options, function(err, response) {
$modalOverlay.hide();
$cartCounter.text(response.data.cart.items.length + " items in your cart");
$modalContent.html(response.content);
});

The code examples are using ES6 syntax. You can see other examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Related

HTTP requests does not work on my real android device (React Native Expo Tunnel)

I'm facing this issue while posting data to my server via axios.post(). It always catches the error "Network Error" when I run my app on my real android device. But when I use an android emulator device, it works correctly and returns the response. I tried to use "ngrok http 5000" and used the uri that ngrok gave me but that didn't work either. I'm hopeless at this point because my app is using QR code scanner and indie notifications. I'm unable to test it while using emulator. Please help me!
I'm leaving my axios.post() method below
const login = async (email, password, navigation) => {
userState.loading = true;
axios
.post(`http://${localIP}:5000/api/user/login`, {
email,
password,
})
.then((response) => {
userState.loading = false;
userState.user = response.data;
console.log("Data: ", response.data);
navigation.reset({
index: 0,
routes: [{ name: "Home" }],
});
})
.catch((err) => {
userState.loading = false;
console.log("ERR :", err.message);
});
};
I tried to use "ngrok http 5000" and used the uri that ngrok given me but that didn't work either.
Might be a little light on information in order to give a definite answer, it's worth looking at installing a debugger and inspecting the network requests to see where they're going wrong. But here's a couple things to try:
In your AndroidManifest.xml file add the line: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Use http://my.local.ip:port instead of http://localhost:5000 (looks like you're doing this anyway, if not you get this via ipconfig in Command Prompt)
Try adding "Content-Type": "application/json", Accept: "application/json" headers to your Axios request

Clear browser cookies React Native 0.60

My app is using Instagram's REST API, in order for a user to logout and login with a different account I have to clear the cookies for www.instagram.com from the browser. I have been using react-native-cookie with RN 0.59.10 and it has been working fine.
After upgrading to RN 0.60 I can't use the react-native-cookie or any of its alternate packages because they don't support auto-linking. The solution I have found is using the RCTNetworking module from the react-native library. You can see the solution here.
Code
var RCTNetworking = require('RCTNetworking');
export const logout = () => {
return new Promise((resolve, reject) => {
RCTNetworking.clearCookies(result => {
if (!result) {
console.log('Error Message');
reject()
}
store.dispatch({ type: "RESET_APP_STATE" });
NavigationService.navigate("AuthLoading");
resolve()
});
});
};
The code runs fine. The app's state is cleared and the user is navigated to the login screen, but when I open the Instagram page in the webView instead of asking for the username and password, it directly logs me in.
You should use the community-version of react-native-cookies which can be found here: https://github.com/react-native-community/react-native-cookies
I am happily using this in combination with version 0.61.5 of React-Native.

How to shorten url which contains Id and parse that id when the url is clicked

I am using linking to link my react-native app to my domain
https://www.example.com/
I am also passing an id which changes based on the article shared
So the shared url looks like:
https://www.example.com/12234503
What i want to do is shorten this using firebase dynamic links(I can only use this)
I have no clue how to pack this id into the createShortDynamicLink function or how to access the id i got when i will open this short link in my app
This is what my code without firebase links looks right now
Accepting the url
componentDidMount() {
Linking.addEventListener('url', this.handleUrl);
Linking.getInitialURL().then((url) => {
if (url) {
Linking.openURL(url);
}
}).catch(err => console.error('An error occurred', err));
DeepLinking.addScheme('https://');
DeepLinking.addRoute('www.example.com/:id', (response) => {
this.props.navigation.navigate('Article',{id:response});
});
So i open the app and then take the use to the page where i use the id to load the article
Sharing the Url using react-native-share
let shareOptions = {
title: "Share This Story",
message: "Read This Awsome Article:",
url: "https://www.example.com/"+this.state.id,
subject: "Share Link"
};
Share.open(shareOptions).catch((err) => { err && console.log(err); });
Can you give me a rough outline of code that shows me how can i implement the same using firebase dynamic links
All this is because i want to shorten the shared link
I have a domian to confige
Have you tried this?
url: `https://www.example.com/${this.state.id}`

Linking in react native can open just one app

UPDATE 1
I removed return from code and now links work on IOS.
But on android I can't open any app. Any idea?
I am trying to open different apps from my app.
return Linking.openURL(“twitter://“);
return Linking.openURL(“instagram://“);
But it doesn’t work. I configured IOS by documentation. On android doesn’t work too. While...
return Linking.openURL(“tripadvisor://“);
Work just fine.
Any idea why I can’t open other apps.
This is code that I am using (open app if installed or open store with it but sometimes even store doesn't open) what I did wrong:
let appUrl = "instagram://";
Linking.canOpenURL(appUrl).then(supported => {
if (!supported) {
Alert.alert("",
"",
[
{text: "go to store", onPress: this.openStorePress},
{text: "cancel", onPress: () => { }, style: 'cancel'},
],
{ cancelable: false }
);
} else {
return Linking.openURL(appUrl);
}
}).catch(err => {
console.error(err);
});
Your issue is related to the content of the url, twitter:// means nothing for the Android Twitter app, so it will not open.
For example, the following code should work:
Linking.openURL('twitter://timeline')
or
Linking.openURL('instagram://user?username=apple')
You have to find the rights url schemes (documentations are not very clear about it) that may be different between iOS and Android.
Twitter: How can I open a Twitter tweet using the native Twitter app on iOS?
Instagram: https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ (all do not work on Android)
misc: https://pureoxygenlabs.com/10-app-url-schemes-for-marketers/
You have to find the rights URL schemes. Have look at my code
Instagram
Linking.openURL('instagram://user?username=apple')
.catch(() => {
Linking.openURL('https://www.instagram.com/apple');
})
Twitter
Linking.openURL('twitter://user?screen_name=apple')
.catch(() => {
Linking.openURL('https://www.twitter.com/apple');
})
Facebook
Linking.openURL('fb://page/PAGE_ID');
Linking.openURL('http://instagram.com/_u/USER_NAME');
Linking.openURL('http://instagram.com/_p/PICTURE');
Your code looks pretty solid, here's an example of how I open twitter in my app.
const twitterUrlScheme = `twitter://user?screen_name=${twitterUsername}`;
Linking.canOpenURL(twitterUrlScheme)
.then((supported) =>
Linking.openURL(
supported
? twitterUrlScheme
: `https://www.twitter.com/${twitterUsername}`
)
)
.catch((err) => console.error('An error occurred', err));
I think perhaps your issue might be the return Linking.openUrl, I'm not sure you need the return in that statement. Does it work if you remove the return? Otherwise, it might help to move your Alert outside of the then-block from canOpenUrl.
I have used only url and it's working both iOS and android
Linking.openURL('https://www.facebook.com/');
You haven't completed the " fot twitter and instagram, I don't know whether you made the same mistake in app too, if yes, fixing that might solve it.
Try to use a package like:
https://github.com/react-native-community/react-native-share
You can try to use only some of it's functions or look into the native code from there and create some bridge functions in the native code and then export them to be used in JS code.
Note: you will have to use real devices for the tests.

Mocha, using this.skip() to skip tests dynamically doesn't work

I'm trying to skip tests if a condition returns true using this.skip() inside an "it", but I'm getting an error "this.skip is not a function".
this is the simple code I'm trying to check it on:
var async = require('async');
var should = require('chai').should();
var chai = require('chai'),
should = chai.should();
expect = chai.expect;
chai.use(require('chai-sorted'));
describe('Testing skip...\n', function() {
this.timeout(1000);
it('test1', (done) => {
this.skip();
console.log("1")
done();
});
it('test2', (done) => {
console.log("2");
done();
});
});
I installed "mocha#5.2.0 " since I saw it only works on since "mocha v3.0.0",
but I still cant get it to work, and none of the past discussions on the subject seems to fix my problem.
in order to use this in mocha, don't use arrow function. So, in your code you need to write it as
it('test1', function(done) { // use regular function here
this.skip();
console.log("1")
done();
});
The best practice in Mocha is to discourage arrow function as described in https://mochajs.org/#arrow-functions
Hope it helps