gpt4 book ai didi

javascript - CollectionFS和Meteor : how to join uploaded files with existing collection

转载 作者:行者123 更新时间:2023-12-03 11:36:46 26 4
gpt4 key购买 nike

我有由 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}});
}
});
}
});

如果您有任何其他方法建议我,请告诉我。

最佳答案

这可以帮助: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);
}
});
}
});

关于javascript - CollectionFS和Meteor : how to join uploaded files with existing collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26464072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com