我有一个像这样的自定义对象:
function BlobModel(id){
this._id = id;
this._rev = null;
this.filename = '';
this.index = '';
this.data = '';
}
如果我把它放在我的邮袋数据库中它可以正常工作,但我有一个奇怪的问题,如果我这样做:
var myDoc = new BlobModel('myId');
db.put(myDoc).then(function(){
console.log(myDoc);
})
console.log结果:
为什么我将 _rev值和 _rev_tree属性添加到我的myDoc
参数中,它应该只添加到db中的文档中。为什么它与本机对象的行为不同?怎么解决?我看到的唯一方法是在放置它之前克隆我的对象,但是在重物上它很奇怪。
如果我使用本机对象执行此操作,则不会对其进行修改:
var myDoc = {
_id: 'myId',
_rev: null,
filename: '',
index: '',
data: ''
}
db.put(myDoc).then(function(){
console.log(myDoc);
});
答案 0 :(得分:1)
答案 1 :(得分:1)
PouchDB希望您插入纯JavaScript对象作为文档。如果您尝试插入自定义类,那么克隆算法将忽略它,您将开始看到奇怪的错误。
我的建议是在将JSON.parse(JSON.stringify(myDoc))
插入PouchDB之前运行var canvas = document.getElementById("canvasTop");
// set the canvas element's width/height to cover #wrapper
var wrapper=document.getElementById('wrapper');
var wrapperStyle=window.getComputedStyle(wrapper,null);
canvas.width=parseInt(wrapperStyle.getPropertyValue("width"));
canvas.height=parseInt(wrapperStyle.getPropertyValue("height"));
//
var ctx = canvas.getContext("2d");
ctx.lineWidth = 10;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.fillStyle = "skyblue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var img=new Image();
img.src= "els.png";
ctx.drawImage(img, (canvas.width-img.width)/2, (canvas.height-img.height)/2, 990,172);
// set "erase" compositing once at start of app for better performance
ctx.globalCompositeOperation = "destination-out";
var canvasOffset = $("#canvasTop").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var startX;
var startY;
var isDown = false;
function handleMouseDown(e) {
e.preventDefault();
startX = parseInt(e.clientX - offsetX);
startY = parseInt(e.clientY - offsetY);
isDown = true;
}
function handleMouseUp(e) {
e.preventDefault();
isDown = false;
}
function handleMouseOut(e) {
e.preventDefault();
isDown = false;
}
function handleMouseMove(e) {
if (!isDown) {
return;
}
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
// Put your mousemove stuff here
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
startX = mouseX;
startY = mouseY;
}
$("#canvasTop").mousedown(function (e) {
handleMouseDown(e);
});
$("#canvasTop").mousemove(function (e) {
handleMouseMove(e);
});
$("#canvasTop").mouseup(function (e) {
handleMouseUp(e);
});
$("#canvasTop").mouseout(function (e) {
handleMouseOut(e);
});
。要么是,要么不使用自定义类。