【发布时间】:2014-12-08 02:13:54
【问题描述】:
我想将 HTML div(及其所有内容)放入 JSON 对象并将其保存到 PouchDB 数据库。
我的 div 看起来像这样:
<div class="easel">
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(85, 222, 25);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(164, 101, 52);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: rgb(214, 15, 77);"></div><div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
<div class="square" style="background-color: white;"></div>
</div>
我写了一个这样的函数:
window.saveBoard = function() {
var db = new PouchDB('tiles');
var remoteCouch = false;
var current_layout = $('.easel');
var new_board = {
_id: new Date().toISOString(),
board: current_layout
}
db.put(new_board, function callback(err, result) {
if(!err) {
console.log("Successfully saved new board.");
}
})
}
但它似乎没有做我想做的事,我在控制台中收到此错误:Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned.
我需要做些什么来存储这个 div 信息,以便稍后将其取回以馈送到页面中?
【问题讨论】:
-
您可能希望使用
$('.easel').html()而不是将元素作为对象来获取HTML。不过可能与您的问题无关。 -
现在好像正在保存,谢谢。我从数据库中提取的结果如下所示: Promise {cancel: function, [[PromiseStatus]]: "resolved", [[PromiseValue]]: Object}cancel: function (){return this}__proto__: Promise[ [PromiseStatus]]: "resolved"[[PromiseValue]]: Objectoffset: 0rows: Array[3]total_rows: 3__proto__: Object 你知道我怎样才能用那个 [[PromiseValue]] 东西得到 div 数组吗?
-
您是使用 jQuerys Ajax 还是 Angulars $http 来执行 GET 请求?您列出的那些方法只是调用时的承诺,看起来不像来自服务器的实际数据。
-
不,我不是。根据我在 PouchDB 文档 (pouchdb.com/api.html) 中看到的内容,我只需要 db.allDocs。
-
啊,对不起。我看到了你的新问题,但没有意识到这就是你得到结果的方式。我没有使用 PouchDB 的经验,所以我会让其他人回答,祝你好运!
标签: javascript jquery pouchdb