【问题标题】:PouchDB Update Document: DataCloneErrorPouchDB 更新文档:DataCloneError
【发布时间】:2017-04-06 12:21:11
【问题描述】:

这是我已经研究了一段时间的代码。出于某种原因,它返回给我以下错误:

Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned.

这甚至在我尝试使用 upsert 插件之前就已经发生了。

db.get(id).then(doc => {
    console.log(doc);
    return db.upsert(id, doc => {
        doc.exp_date = moment(doc.exp_date).add(parseInt(document.getElementById('ext_date').value), 'years');
        return doc;
    }).then(res => console.log(res)).catch(err => console.log(err));
})

我可以知道这个错误的解决方法吗?

【问题讨论】:

    标签: javascript indexeddb pouchdb


    【解决方案1】:

    无法克隆 Moment 实例。试试:

    postMessage(moment(0), '*'); // also throws DataCloneError DOMException
    

    logic for cloning 不允许复制功能,可能就是这种情况。比较:

    postMessage({f: function(){}}); // also throws DataCloneError
    

    然后检查:

    typeof moment(0)._locale.ordinal; // "function"
    

    您需要将add() 返回的对象转换为可以克隆的对象,例如日期、数字等。

    【讨论】:

    • 我解决了这个问题,但方式有点不同。我得到了关于 PouchDB 问题的答案。我只是用moment(...).add(...)._d
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    相关资源
    最近更新 更多