Skip to main content

Installing JavaScript SDK for Data Storage and Instant Messaging

Installing SDK

Depending on the platform you're using, there are different ways for you to install the SDK.

Web

The following method can be used if your project runs in browser, WebView, or other in-app HTML platforms.

Installing and Referencing SDK

npm

The easiest way to integrate our SDK into your project is to use npm:

$ npm install leancloud-storage --save

Create references to the SDK with require:

const AV = require("leancloud-storage");
const { Query, User } = AV;
CDN

You can also import the SDK with a script tag:

<script src="//code.bdstatic.com/npm/leancloud-storage@4.13.2/dist/av-min.js"></script>

When using this method, you can reference the SDK with the global variable AV:

const { Query, User } = AV;

Node.js

JavaScript SDK also works with Node.js. If you plan to use LeanStorage within LeanEngine, you can import leanengine into your project by following instructions in LeanEngine Quick Start.

Installing and Referencing SDK

You can use npm to install and reference our SDK for Node.js as well. See npm for more details.

React Native

You can use npm to install and reference our SDK for React Native. React Native support is provided by a separate adapter library (@leancloud/platform-adapters-react-native).

Installing:

# Step 1: Install
$ yarn add leancloud-storage @leancloud/platform-adapters-react-native @react-native-community/async-storage@1

# Step 2: Link
# For React Native 0.60+
$ npx pod-install
# For React Native <= 0.59
# npx react-native link @react-native-community/async-storage
# For Expo (SDK >= 38) 无需 link

Referencing:

import AV from "leancloud-storage/core";
import * as adapters from "@leancloud/platform-adapters-react-native";
AV.setAdapters(adapters);

Electron

Electron uses npm for managing dependencies. You can install our SDK with the following command:

$ npm install leancloud-storage --save

Importing SDK as Browser Scripts

You can import the SDK by adding a script tag into index.html:

<script src="./node_modules/leancloud-storage/dist/av-min.js"></script>

Importing SDK as Node.js Modules

The way of using script to import the SDK works in most scenarios. However, if you have the following needs, you can also use require('leancloud-storage') to import the SDK as Node.js modules:

  • Using the SDK in the main process
  • Constructing AV.File with Buffer or Stream provided by AV.File
note

require and script import two different sets of SDK. Each of them needs to be initialized separately and cannot be used interchangeably.

Other

The JavaScript SDK provides platform-independent versions to support other platforms. All platform-related APIs are abstracted into configurable adapters, which need to be configured for the target platform after introducing the SDK core. Assuming there is an adapter package (platform-adapters-xyz) for a platform (xyz) on npm, you can configure the JavaScript SDK as follows:

const AV = require("leancloud-storage/core");
const adapters = require("platform-adapters-xyz");
AV.setAdapters(adapters);

Precompiled UMD files are also available, in case you cannot or do not want to use npm:

https://code.bdstatic.com/npm/leancloud-storage@4.13.2/dist/av-core-min.js

You can implement your own adapter for a target platform. Interface definitions for adapters can be found in the @leancloud/adapter-types package. You can also search for adapters contributed by other developers with the keyword platform-adapters .

Initializing Your Project

To begin with, get your App ID and App Key from your app's

AV.init({
appId: "your-app-id",
appKey: "your-app-key",
serverURL: "https://your_server_url",
});

Enabling Debug Logs

For Node.js applications:

DEBUG=leancloud* npm start

For browsers:

localStorage.setItem('debug', 'leancloud*');

You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to IDE, browser console, or your app's Dashboard > LeanEngine > App logs.

const AV = require("leancloud-storage");
AV.debug.enable();
AV.debug.disable();
caution

Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.

Verifying

First of all, make sure you are able to connect to LeanCloud server. You can test it by running the following command:

curl "https://{{host}}/1.1/date"

If everything goes well, it will return the current date:

{ "__type": "Date", "iso": "2020-10-12T06:46:56.000Z" }

Then add the following code into your project:

const TestObject = AV.Object.extend("TestObject");
const testObject = new TestObject();
testObject.set("words", "Hello world!");
testObject.save().then((testObject) => {
console.log("保存成功。");
});

Save the file and run it.

Then go to your app's Dashboard > LeanStorage > Data > TestObject. If you see the following content, it means that you have correctly installed the SDK.

Debugging

This guide is written for the latest version of our SDK. If you encounter any error, please first make sure you have the latest version installed.

401 Unauthorized

If you get a 401 error or see the following content in network logs:

{
"code": 401,
"error": "Unauthorized."
}

It means that the App ID or App Key might be incorrect or don't match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such error.

Client Cannot Access the Internet

Make sure you have granted enough permissions for your mobile app.