【发布时间】:2015-07-17 20:10:30
【问题描述】:
在我的用户个人资料集合中,我有一个包含图像对象的数组。
一个用户的个人资料集合中最多可以有 3 张图片。如果用户有 3 个,则抛出已达到最大值的错误。用户可以选择在前端自己删除图像。
我认为解决方案是使用 $size 检查数组的长度。如果小于 3,则插入图片,否则抛出错误。
我正在使用 tomi:upload-jquery 包。
客户:
Template.uploadImage.helpers({
uploadUserData: function() {
return Meteor.user();
},
finishUpload: function() {
return {
finished: function(index, fileInfo, context) {
Meteor.call('insert.profileImage', fileInfo, function(error, userId) {
if (error) {
// todo: display modal with error
return console.log(error.reason);
} else {
// console.log('success ' +userId);
// console.log('success ' + fileInfo);
}
});
}
};
}
});
我使用的方法(服务器):
'insert.profileImage': function(postImage) {
check(postImage, Object);
// check array profile.images max 3
Meteor.users.update(this.userId, {
$push: {
'profile.images': postImage
}
});
},
【问题讨论】:
-
第四张图片是不允许的,还是应该替换之前的图片?您的模板代码正在更新配置文件,这让客户非常信任。您是否在应用程序的其他任何地方都这样做?我问是因为这里更好的解决方案是使用配置文件更新的方法。
-
因此,如果我理解正确,您要求的解决方案不包括替换
images数组中的项目?您应该在问题本身中对其进行编辑,这不是很清楚。 -
@DavidWeldon 嗯,是的,我实际上在几个地方进行了更新。但是我这里用的方法对吗?
-
@Kyll 让问题更清楚