我有autoform创建的门票收藏。创建一个故障单后,我需要上传一个与附件相关的文件。当文件与任何人没有关系时,一切正常。但是当我尝试使用当前对象ID在集合中添加字段时,不会出现任何内容。 我的代码:
收集:
tickets = new Mongo.Collection("tickets");
var Schemas = {};
Schemas.tickets = new SimpleSchema({
title: {
type: String,
label: "Titolo"
},
description: {
type: String,
label: "Descrizione"
},
deadline: {
type: Date,
label: "Deadline prevista"
},
user: {
type: String,
autoValue:function(){ return this.userId; }
},
status: {
type: String,
autoValue: function(){ return "open"; }
}
});
tickets.attachSchema(Schemas.tickets);
SimpleSchema.messages({
"required title": "Il [label] è richiesto",
"required description": "La [label] è richiesta",
"required deadline": "La [label] è richiesta"
});
//
Files = new FS.Collection("Files", {
stores: [new FS.Store.FileSystem("Files", {path: "~/uploads"})]
});
和模板:
Template.dashboard.events({
'click #fileUpload' : function(event, template) {
var file = template.find('.myFileInput').files[0];
var ticket = this._id;
Files.insert(file, function (err, fileObj) {
if (err) {
console.log(err);
} else {
console.log(ticket);
tickets.update({_id: ticket}, {$push: {files: ticket}});
}
});
}
});
如果您有任何其他方法建议我,请告诉我。
答案 0 :(得分:1)
这有助于:https://github.com/CollectionFS/Meteor-CollectionFS#storing-fsfile-references-in-your-objects
或者,您可以为文件集合创建“元数据”字段,然后将其与故障单集合连接起来:
Template.dashboard.events({
'click #fileUpload' : function(event, template) {
var file = template.find('.myFileInput').files[0];
var ticket = this._id;
var newFile = new FS.File(file);
newFile.metadata = {
ticketId: ticket //ticket id - so you know that this file is from particular ticket, you can then find it by Files.find({'metadata.ticketId': ticket});
};
Files.insert(newFile, function (err, fileObj) {
if (err) {
console.log(err);
}
});
}
});