将数据添加到 JSONStore 集合

improve this page | report issue


设置 React Native 开发环境

遵循 React Native 入门页面中提供的指示信息来设置您的机器进行 React Native 开发。

将 JSONStore SDK 添加到 React Native 应用程序

npm 中,JSONStore SDK for React Native 作为 React Native 模块提供。

开始使用新 React Native 项目

  1. 创建新 React Native 项目。
     react-native init MyReactApp
    
  2. 将 MobileFirst SDK 添加到您的应用程序。
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
    
  3. 将所有本机依赖关系添加到您的应用程序。
    react-native link
    

将数据添加到 JSONStore 集合

在您的 App.js 内,必须导入以下包:

import { JSONStoreCollection, WLJSONStore } from 'react-native-ibm-mobilefirst-jsonstore';

将数据添加到 JSONStore 集合时需要执行三个步骤:

  1. 创建新集合,您可以通过调用 JSONStoreCollection 构造函数来创建新集合,如下所示:
     var favourites = new JSONStoreCollection('favourites');
    
  2. 打开集合,如果不打开新创建的集合,则无法使用该集合执行任何操作。要打开集合,请调用 WLJSONStore 的 openCollections API。请参阅下面的样本代码。
    WLJSONStore.openCollections(['favourites']).then(data => { console.log(data); }).catch(err =>{ console.log(err); });
    
  3. 将数据添加到集合,在您打开集合之后,向内或向外启动数据事务。您可以使用以下 API 将数据添加到打开的集合。
     var favCollection = new JSONStoreCollection('favourites');
     favCollection.addData(myJsonData)
     .then(data => {
     	console.log("Succesfully added data to collection!"));
     .catch(err => {
     	console.log("Error while adding data to collection. Reason : " + err);
     });
    
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020