Backendless is undefined when used in React-Native application

Dev tools: Visual Studio Code + Typescript + React-Native
I am trying to init backendless library:

import { Backendless } from 'backendless';
...


Backendless.initApp(APP_ID, APP_KEY, APP_VER);

I get this error:
Cannot read property ‘initApp’ of undefined

I read a couple of similar issues on this forum. At the time these issues where raised, the suggested solution was to wait for a new backendless version where those issues would be addressed. I have the latest version (3.1.22).

Is Backendless compatible with React-Native? If yes how to integrate them?

(Side-note: how about the use of typescript? Is it straight-forward or should I take into account certain things?)

Thank you.

This is my package.json:


{
  "name": "__app__name__",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "backendless": "^3.1.22",
    "native-base": "^0.5.18",
    "react": "15.4.1",
    "react-native": "0.39.2"
  },
  "devDependencies": {
    "@types/react-native": "^0.37.1",
    "babel-jest": "18.0.0",
    "babel-preset-react-native": "1.9.1",
    "jest": "18.0.0",
    "react-test-renderer": "15.4.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

Hi Seya,

try to import it next way:

import Backendless from 'backendless';

It is absolutely compatible with TypeScript.
Unfortunately we do not have an example of using the JS SDK with React Native yet, but you can find here (click me) example of using it with TypeScript.

Regards,
Stanislaw

Thanks! I was able to load Backendless and even use it with typescript.

Sorry to come back to again. Now I am facing another problem. I have successfully initialized backendless library. But Backendless.UserService is undefined. This is the code

Backendless.UserService.login( email, password, true, new Backendless.Async(succ =>{

 alert('Success');

 }, err =>{

 alert(''Failed');

 }))

This above code crashes with error: Cannot read property ‘login’ of undefined

Please make sure that you call Backendless.initApp before calling Backendless.UserService.login. Can you confirm that?

I solved like this:

  1. First I changed the name of Backendless.d.ts definition file into index.d.ts and placed it in this folder:
./node_module/@typings/backendless

This way vs code will pick up the types.
2. Inside index.d.ts, I change the export statement from:

declare module 'backendless' { 
 import Backendless = __Backendless;
 export Backendless;
}

to this code:

declare module 'backendless' { 
 import Backendless = __Backendless;
 export default Backendless;
}

This will allow me to import backendless in my typescript file this way:

import Backendless from 'backendless'; 

and still have types and intellisense (otherwise vs-code complaints “module backendless does not have a default export”).

So I understand that it is not a problem with the Backendless.js library but how to use it properly along with typescript.