Consulta de datos desde la recopilación de JSONStore

improve this page | report issue

Configuración del entorno de desarrollo de React Native

Siga las instrucciones que se proporcionan en la página de iniciación de React Native para configurar la máquina para el desarrollo de React Native.

Adición de un SDK JSONStore en la aplicación React Native

El SDK de JSONStore para React Native está disponible como módulo de React Native en npm.

Iniciación con un nuevo proyecto de React Native

  1. Cree un nuevo proyecto de React Native.
     react-native init MyReactApp
    
  2. Añada el SDK de MobileFirst a la aplicación.
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
    
  3. Enlace todas las dependencias nativas a su aplicación.
    react-native link
    

Consulta de datos desde la recopilación de JSONStore

Raramente deseará obtener todos los documentos de una colección al mismo tiempo. En general, necesita la capacidad de consultar los datos existentes en su recopilación.

En App.js debe importar los siguientes paquetes:

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

Hay dos pasos para consultar los datos de una recopilación de JSONStore:

  1. Abrir una recopilación: abrir una recopilación le permite interactuar con ella.
     WLJSONStore.openCollections(['favourites']).then(data => { console.log(data); }).catch(err =>{ console.log(err); });
    
  2. Captar datos de una recopilación: tras abrir una recopilación, puede captar los documentos según una consulta determinada. Para consultar JSONStore, se proporcionan dos clases con las que trabajar: JSONStoreQuery y JSONStoreQueryPart.
    Puede utilizar varios objetos JSONStoreQueryPart para la misma llamada pasando cada objeto JSONStoreQueryPart en una matriz. Los distintos objetos JSONStoreQueryPart se unen mediante una sentencia OR. Las distintas condiciones de un objeto JSONStoreQueryPart se unen mediante una sentencia AND.

    Consulte el código siguiente:

     var favCollection = new JSONStoreCollection('favourites');
     var queryPart1 = new JSONStoreQueryPart();
     queryPart1.addBetween("age", 21, 50);
    
     var queryPart2 = new JSONStoreQueryPart();
     queryPart2.addEqual("gender", "female");
    
     // Tenga en cuenta cómo varios objetos JSONStoreQueryPart se pasan en una matriz para crear una consulta completa
     // La llamada siguiente devolverá todos los documentos que tienen
     // "gender" establecido en "female" OR "age" entre 21 y 50
    
     favCollection.findDocuments([queryPart1, queryPart2])
     .then(data => {
     	console.log("Succesfully fetched all documents from collection!"));
     	console.log("Data: " + JSON.stringify(data));
     .catch(err => {
     	console.log("Error while fetching data from 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 February 27, 2020