【问题标题】:Why could not load data from Adapter into JSONStore?为什么无法将数据从 Adapter 加载到 JSONStore?
【发布时间】:2014-07-20 01:48:18
【问题描述】:
function getListPhoneNumbers() {

    var data = {listContacts:[{name:'Ho Cong Vi',number:'12345666'},{name:'hcv',number:'6543218'}]};

    WL.Logger.info('Data:'+JSON.stringify(data));
    return data;
}

function addListPhoneNumber(data) {

    WL.Logger.debug('Add Data to JSONStore: ' + data);
    return;
}

function updateListPhoneNumber(data) {

    WL.Logger.debug('Updata Data from JSONStore: ' + data);
    return;

}


function deleteListPhoneNumber(data) {


    WL.Logger.debug('Delete Data from JSONStore: ' + data);
    return;

}

这是我在main.js中的代码:

$('#show-all-btn').on('click', showAllData);

var collectionName = 'Contacts',

collections = {};
collections[collectionName] = {
    searchFields: {
        name: 'string',
        number: 'string'
    },
    adapter: {
        name: 'listPhoneNumbers',
        add: 'addListPhoneNumber',
        replace: 'updateListPhoneNumber',
        remove: 'deleteListPhoneNumber',
        load: {
            procedure: 'getListPhoneNumbers',
            param: [],
            key: 'listContacts'
        }
    }
};

WL.JSONStore.init(collections)

    function showAllData() {

    $('#show-all-btn').on("click", function() {
        $('#info').show();
    });
    WL.JSONStore.get(collectionName).load().then(function(res) {
        alert('ok' + JSON.stringify(res));
    }).fail(function(errorObject) {
        alert(errorObject);
    });
}

这是错误:

[wl.jsonstore] {"src":"load","err":18,"msg":"FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_L‌​OAD_OBJ","col":"Contact","usr":"jsonstore","doc":{},"res":{}

【问题讨论】:

  • 这是错误:[wl.jsonstore] {"src":"load","err":18,"msg":"FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_LOAD_OBJ","col":"Contact","usr ":"jsonstore","doc":{},"res":{}

标签: ibm-mobilefirst jsonstore


【解决方案1】:

错误消息是说您传递的加载对象无效。这可能是因为您传递了param 而不是params。注意末尾的s。

另外,这段代码:

WL.JSONStore.init(collections)

    function showAllData() {

    $('#show-all-btn').on("click", function() {
        $('#info').show();
    });
    WL.JSONStore.get(collectionName).load().then(function(res) {
        alert('ok' + JSON.stringify(res));
    }).fail(function(errorObject) {
        alert(errorObject);
    });
}

看起来不对,可能你的意思是这样写的:

WL.JSONStore.init(collections).then(function () {

  WL.JSONStore.get(collectionName).count().then(function (numberOfDocsInCollection) {
    if(numberOfDocsInCollection < 1) {
      WL.JSONStore.get(collectionName).load().then(function(res) {
        //handle success
      })
    }
  })
});

为简洁起见,我省略了处理失败。请注意,如果集合中的项目已经存在,load 将复制这些项目,因此检查集合是否为空的计数。

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 2022-06-13
    • 2022-11-07
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多