WLAuthorizationManager.obtainAccessToken("").then((token)=>{console.log('--> pingMFP(): Success ',token);varresourceRequest=newWLResourceRequest("/adapters/javaAdapter/resource/greet/",WLResourceRequest.GET);resourceRequest.setQueryParameters({name:"world"});resourceRequest.send().then((response)=>{// Will display "Hello world" in an alert dialog.
alert("Success: "+response.responseText);},(error)=>{alert("Failure: "+JSON.stringify(error));});},(error)=>{console.log('--> pingMFP(): failure ',error.responseText);alert("Failed to connect to MobileFirst Server");});
IBM Mobile Foundation JSONStore 是一个可选的客户机端 API,提供轻量级、面向文档的存储系统。JSONStore 支持持久存储 JSON 文档。即使设备处于脱机状态,在 JSONStore 中也可以使用应用程序中的文档。这种持久的、始终可用的存储很有用,例如,当设备中没有可用的网络连接时,使用户能够访问文档。
安装
针对 React Native 应用程序的 IBM MobileFirst JSONStore SDK 依赖于 IBM MobileFirst Foundation SDK。将 IBM MobileFirst Foundation SDK 添加到您的应用程序。
vardogs=newJSONStoreCollection('dogs');WLJSONStore.openCollections(['dogs']);// Provide the name of the collection as a string.
将数据添加到集合
将 JSON 数据添加到您的集合。
varhachi={"name":"Hachiko","breed":"Akita","country":"Japan"};dogs.addData(hachi).then(()=>{// Data was added to the collection successfully.
}).catch((err)=>{// Error adding data. See the err object for more information on the error
})
从集合查询数据
使用任何 JSONStoreCollection.find* API 查询集合。使用 JSONStoreQueryPart 和 JSONStoreQuery API 对 JSON 数据进行高级过滤和查询。
dogs.findAllDocuments().then((result)=>{// result will have all the documents in the collection
// E.g. [ {"json": {"name":"Hachiko","breed":"Akita","country":"Japan"},"_id":2}]
}).catch((error)=>{console.error("Error finding docs "+JSON.stringify(error));});
关闭、清除和销毁您的集合。
关闭您的集合将关闭您的 JSONStore 集合,要进一步访问,需要使用 openCollections API 重新将其打开。
WLJSONStore.closeAll()
清除集合将从集合中移除所有文档,但不会将其销毁。
dogs.clearCollection().then(()=>{// All documents cleared successfully
}).catch((err)=>{// An error occurred while clearing the collection.
})