Javascript update({u id:id},{$set:{imgName:name,imgBase64:buffer});在控制台中响应0
我正试图用文件阅读器将图像上传到MongoDB, helper中的代码如下所示:Javascript update({u id:id},{$set:{imgName:name,imgBase64:buffer});在控制台中响应0,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我正试图用文件阅读器将图像上传到MongoDB, helper中的代码如下所示: Template.Producto.events({ 'change #file-upload': function (event, template) { var file = event.target.files[0]; var name = event.target.files[0].name; var id = this._id._str; if (!file) retu
Template.Producto.events({
'change #file-upload': function (event, template) {
var file = event.target.files[0];
var name = event.target.files[0].name;
var id = this._id._str;
if (!file) return;
// Only process image files.
if (!file.type.match('image.*')) {
alert('Only image files are allowed');
return;
}
var reader = new FileReader();
reader.onload = function (file) {
var result = reader.result;
Meteor.call('saveFile', name, result, id);
}
Session.set('file', name);
reader.readAsDataURL(file); //read the file as base64
console.log(id);
console.log(name);
console.log(file);
}
});
我的方法是:
Meteor.methods({
'saveFile': function (name, buffer, id, data) {
console.log(id);
console.log(name);
console.log(buffer);
Productos.update({_id: id}, {
$set: {
imgName: name, imgBase64: buffer
}
})
}
});
问题是,我也无法从前端控制台更新文档
如果我从控制台更新,控制台将返回一个0号
另外,我的文档在id上有一个多维数组。product对象来自一个来自json文档的mongoimport,因此可以通过这种方式访问id
如果我尝试使用_str访问id,控制台将返回一个错误:
errorClass
{
error: 403,
reason: "Not permitted. Untrusted code may only update documents by ID.",
details: undefined,
message: "Not permitted. Untrusted code may only update documents by ID. [403]",
errorType: "Meteor.Error"
}
我的产品如下
Productos = new Mongo.Collection('productos');
Productos.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc) {
return true;
},
remove: function(userId, doc) {
return true;
}
});
我做错了什么?您是如何尝试使用_str访问id的?数据是否存储在MongoDB数据库中?我正在访问Template.Producto.events的第5行中的id,如var id=this.\u id.\u str;另外,我知道我得到了id,因为我将它发送到服务器和客户端上的控制台bot,并在那里反映出来,还有一点需要指出,我修改了代码以插入而不是更新,代码:Productos.insert{imgName:name,imgBase64:buffer};更新{u id:id},{$set:{imgName:name,imgBase64:buffer}通常,您不应该自己提供{u id}。你为什么要那样做?如果需要从客户端提供唯一标识符,请使用另一个自定义字段。然而,在大多数情况下,这是不必要的。你没有把身份证放在_str财产里吗?为什么不按它查询?按别名查询,现在正在工作!!!!恭喜!你可能应该删除这个问题,或者发布你的解决方案,并解释出哪里出了问题。