PouchDB更新文档:DataCloneError

时间:2017-04-06 12:21:11

标签: javascript indexeddb pouchdb

这是我一直在研究的代码。出于某种原因,它返回了以下错误:

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));
})

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

1 个答案:

答案 0 :(得分:1)

The Moment instance can't be cloned. Try:

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

The logic for cloning disallows copying functions, which may be the case. Compare with:

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

And check:

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

You'll need to convert the object returned by add() to something that can be cloned, such as a Date, number, etc.