React Native random.browser.js error - react-native

Today I simply cloned my project on the same computer it used to work (same commit). Everything went fine, builded etc. BUT them I receive an error saying Can't find variable: self [in random.browser.js:1:18]
the WHOLE JS FILE (yeah...)
var crypto = self.crypto || self.msCrypto
module.exports = function (bytes) {
return crypto.getRandomValues(new Uint8Array(bytes))
}

Related

how to add asset files to a React Native iOS project

I am currently working on getting a react-native project that is running on Android to work on iOS. The following code uses react-native-blob-util to load files from the following folder android/app/src/main/assets/. So if the function is called with path "/app.js" it will read the file at android/app/src/main/assets/webview/app.js
async function buildLocalAssetResponse(path: string): Promise<Buffer> {
let headers = 'HTTP/1.1 ';
const bundlePath = `webview${path}`;
let data = '';
console.log(bundlePath)
try {
data = await ReactNativeBlobUtil.fs.readFile(
ReactNativeBlobUtil.fs.asset(bundlePath),
'base64',
);
I am trying to figure out how to get this working on iOS exactly, I believe I have to copy the relevant files into an ios assets folder so ReactNativeBlobUtil.fs.asset can find them but I am not sure how to create that folder.

How to initilaze react-native on the view of window manager from android

I'm searching the way to implement running react-native application on both of activity and service in single package.
Running RN app on activity is usual, but can't find the right way to execute another react-native application on the service. The latter app should be run on the foreground service and view on the window manager.
My code roughly looks like:
// ReactNativeWindowService.kt
fun startNewReactWindow(
reactContext: ReactContext,
jsMainModuleName: String?,
bundleAssetName: String?,
module: String
) {
Toast.makeText(reactContext, "START()", Toast.LENGTH_SHORT).show()
val headLayout = createHeadLayout(reactContext)
headsManager.addLayout(headLayout)
launch(UI) {
reactInstanceManager = createReactInstanceManager(reactContext, jsMainModuleName, bundleAssetName)
reactRootView = ReactRootView(reactContext)
reactRootView?.startReactApplication(reactInstanceManager, module, null)
headsManager.addHeadView(bubbleLayout.apply { addView(reactRootView) })
}
}
But, this code produce:
07-10 01:05:07.662 26269-26380/Sample E/ReactNativeJS: Error while starting app: TypeError: undefined is not an object (evaluating 'NativeReactModule.startApp')
07-10 01:05:07.674 26269-26380/Sample I/ReactNativeJS: Running application "index" with appParams: {"rootTag":21}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
07-10 01:05:07.674 26269-26380/Sample E/ReactNativeJS: Application index has not been registered.
Hint: This error often happens when you're running the packager (local dev server) from a wrong folder. For example you have multiple apps and the packager is still running for the app you were working on before.
If this is the case, simply kill the old packager instance (e.g. close the packager terminal window) and start the packager in the correct app folder (e.g. cd into app folder and run 'npm start').
This error can also happen due to a require() error during initialization or failure to call AppRegistry.registerComponent.
I'm sure that code passed right parameters such as jsMainModuleName to create ReactInstanceManager. (I got same parameters as activity's params)
What do I miss in my code? or any other reference/docs/advice that would be helpful to me. Thanks in advance. :)
You have to make sure that your file MainAcitivity.java has a function
protected String getMainComponentName() {
return "your file name for example abc";
}
But in your index.android.js file you register other component
AppRegistry.registerComponent('123', () => 143);
Where it must be like this e.g.
AppRegistry.registerComponent('abc', () => abc);
Try to fix it.

LockModule.init undefined error in auth0 react-android getting started

I'm following the instructions to get auth0 running in react-native android https://auth0.com/docs/quickstart/native/react-native-android
And I am getting the following error
undefined is not an object (evaluating 'LockModule.init') show auth0-lock.js:33
The error implies that the LockModule variable is undefined; by checking the code for auth0-lock.js we can see that the variable is being initialized by the following logic:
var { NativeModules, Platform } = require('react-native');
var LockModule = NativeModules.Auth0LockModule;
According to react-native documentation in order to use a module implemented as a native module you need to register it.
The package needs to be provided in the getPackages method of the MainApplication.java file. This file exists under the android folder in your react-native application directory.
You should have something similar to:
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new LockReactPackage()
);
}
If this is not the root cause of the issue you'll need to update your question with more details.
I ran into this issue while trying to get Auth0 working on iOS. Our fix is here (react-native run-ios to see more detailed messages for the issue; correct header search paths; update podfile with OneSignal; add OneSignal library)
https://github.com/auth0/react-native-lock/issues/133

Using react native with Optimizely

I try to follow documentation in Optimizely to get my react native app (#22.2) working but getting such bug.
MainActivity.java:24: error: cannot find symbol
Optimizely.startOptimizelyWithApiToken("xxxxxx", getApplication());
^
symbol: method startOptimizelyWithApiToken(String,Application)
location: class Optimizely
1 error
:app:compileDebugJavaWithJavac
What is wrong and how can I debug . I try
adb logcat ReactNative:V ReactNativeJS:V
but it's not giving me any information
I an on the engineering team at Optimizely and we've released a brand new product called FullStack that is more geared towards developers. As part of the product we now offer a JavaScript SDK for running experiments in all JavaScript clients, including React Native.
To use you would install our SDK:
npm install optimizely-client-sdk
And then you can split traffic using our activate and track methods.
Here is an example:
var optimizely = require('optimizely-client-sdk');
// Initialize an Optimizely client
var optimizelyClientInstance = optimizely.createInstance({ datafile: datafile });
// ALTERNATIVELY, if you don't use CommonJS or npm, you can install the minified snippet and use the globally exported varible as follows:
var optimizelyClientInstance = window.optimizelyClient.createInstance({ datafile: datafile });
// Activate user in an experiment
var variation = optimizelyClientInstance.activate("my_experiment", userId);
if (variation === 'control') {
// Execute code for variation A
} else if (variation === 'treatment') {
// Execute code for variation B
} else {
// Execute default code
}
// Track conversion event
optimizelyClientInstance.track("my_conversion", userId);
For more information please checkout our developer docs: https://developers.optimizely.com/x/solutions/sdks/introduction/index.html?language=javascript
i sorted problem is more about reading docs and using legacy:
compile ('com.optimizely:optimizely-legacy:+#aar') {
transitive = true
}
and then:
Optimizely.startOptimizely("xxxx", getApplication());

"undefined is not a function" error when using mongoose-sessions and express

When I init the application everything is ok, but when I go to any page
like "localhost/" I get this (double: once for the index.html and once for the javascript requested, see bellow) error:
TypeError: undefined is not a function
at hash (myapp\node_modules\express\node_modules\connect\lib\middleware\session.js:341:10)
at myapp\node_modules\express\node_modules\connect\lib\middleware\session.js:324:24
at Promise.module.exports.SessionStore.get (myapp\node_modules\session-mongoose\index.js:85:22)
at Promise.addBack (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:128:8)
at Promise.EventEmitter.emit (events.js:96:17)
at Promise.emit (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:66:38)
at Promise.complete (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:77:20)
at Query.findOne (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\query.js:1607:15)
at model.Document.init (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\document.js:229:11)
at model.init (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\model.js:196:36)
TypeError: undefined is not a function
at hash (myapp\node_modules\express\node_modules\connect\lib\middleware\session.js:341:10)
at myapp\node_modules\express\node_modules\connect\lib\middleware\session.js:324:24
at Promise.module.exports.SessionStore.get (myapp\node_modules\session-mongoose\index.js:85:22)
at Promise.addBack (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:128:8)
at Promise.EventEmitter.emit (events.js:96:17)
at Promise.emit (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:66:38)
at Promise.complete (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\promise.js:77:20)
at Query.findOne (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\query.js:1607:15)
at model.Document.init (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\document.js:229:11)
at model.init (myapp\node_modules\session-mongoose\node_modules\mongoose\lib\model.js:196:36)
This is the code I'm using:
var express = require('express');
var app = this.__app = express();
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
store: this.__getSessionStore()
,secret: this.__secretSession
}));
app.post('/rpc', this.__manageRequest.bind(this)); // <-- this method is not even called
app.use("/client", express.static('./client/')); //
// gets the index.html, but files in subfolders (which exists) throws the second error in the double error
app.use("/", express.static('./client/production/build'));
app.listen(this.__appPort, this.__appIp);
How I get the session store:
__getSessionStore: function(){
var express = require('express');
var app = this.__app;
var SessionMongoose = require("session-mongoose")(express);
var mongooseSessionStore = new SessionMongoose({
url: this.__mongodbUrl
,interval: 120000
});
return mongooseSessionStore;
}
Express version: 3.0.0
Mongoose-sessions version: 0.2.0
The undefined method is "crc16" in the hash method: in debug is undefined too
function hash(sess) {
return crc16(JSON.stringify(sess, function(key, val){
if ('cookie' != key) return val;
}));
}
In the crc module, there is a if(window) and window is defined, the problem is that I cannot see where it's defined!
Because of this window, it will not export any of the modules and therefore the function is undefined! I just very hackly removed the if(window) to always export to it's module.
Well, that was a bug in the crc#0.2.0 module (https://github.com/alexgorbatchev/node-crc/pull/10), and the express is requiring that exact version.
What exactly is the problem and how to fix it?
thanks a lot
This only worked the first time, then BUM, same error again...
As the dependencies in the session-mongoose are like this (loosely):
"dependencies": {
"mongoose": ">= 3.0"
},
It downloaded the mongoose 3.5.4! Npm should only have downloaded the 3.0, but I don't know what happened.
Now I deleted that and put the mongoose at the same version as in my app: 3.4.0.
I still don't know what's wrong with the 3.5.4 version or if the problem is not being equal, but here is the fix.
So many time lost because of this... :S
Perhaps edit ./node_modules/express/package.json to tell it to use more recent CRC library.
Change the line:
"crc": "0.2.0",
to
"crc": ">= 0.2.0",
and then run
npm install -d
If that doesn't install an updated crc library, manually delete the folder ./node_modules/express/node_modules/crc and try again.
I don't promise this will work but it is worth a shot!