如何在插入时引用另一个集合
How to reference another collection on insert?
我正在尝试弄清楚如何拍摄图像(使用 CollectionFS 的文件)并将图像的 ID 插入我的项目imageId
字段中:
库/集合/项目.js
Items = new Mongo.Collection("items");
Items.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoform: {
type: "hidden",
label: false
},
autoValue: function () { return Meteor.userId() },
},
image: {
type: String,
optional: true,
autoform: {
label: false,
afFieldInput: {
type: "fileUpload",
collection: "Images",
label: 'Select Photo',
}
}
},
imageId: {
type: String
}
}));
库/集合/图像.js
if (Meteor.isServer) {
var imageStore = new FS.Store.S3("images", {
accessKeyId: Meteor.settings.AWSAccessKeyId,
secretAccessKey: Meteor.settings.AWSSecretAccessKey,
bucket: Meteor.settings.AWSBucket,
});
Images = new FS.Collection("Images", {
stores: [imageStore],
filter: {
allow: {
contentTypes: ['image/*']
}
}
});
}
// On the client just create a generic FS Store as don't have
// access (or want access) to S3 settings on client
if (Meteor.isClient) {
var imageStore = new FS.Store.S3("images");
Images = new FS.Collection("Images", {
stores: [imageStore],
filter: {
allow: {
contentTypes: ['image/*']
},
}
});
}
现在我的允许规则是:
服务器/允许.js
Items.allow({
insert: function(userId, doc){return doc && doc.userId === userId;},
update: function(userId, doc){ return doc && doc.userId === userId;},
remove: function(userId, doc) { return doc && doc.userId === userId;},
})
Images.allow({
insert: function(userId, doc) { return true; },
update: function(userId,doc) { return true; },
remove: function(userId,doc) { return true; },
download: function(userId, doc) {return true;},
});
我正在使用自动表单,所以我的表单如下所示:
client/item_form.html
<template name="insertItemForm">
{{#autoForm collection="Items" id="insertItemForm" type="insert"}}
{{> afQuickField name="name" autocomplete="off"}}
{{> afQuickField name="image" id="imageFile"}}
<button type="submit">Continue</button>
{{/autoForm}}
</template>
现在,当我选择浏览并选择图像时,它将在数据库中,我想获取它所拥有的_id
并将其放置在之后创建的Item
中,但是如何获取该特定图像?我认为这是引用图像的好方法。
更新 1
找出 ID 在选择文件后实际上是隐藏的:
<input type="hidden" class="js-value" data-schema-key="image" value="ma633fFpKHYewCRm8">
所以我试图让ma633fFpKHYewCRm8
被安置在ImageId
中作为String
.
更新 2
也许一种方法是使用 FS。文件引用?
我已经解决了同样的问题,相当简单,插入文件后,我只是调用一个执行相关集合更新的方法:
客户端.html
<template name="hello">
<p>upload file for first texture: <input id="myFileInput1" type="file"> </p>
</template>
库.js
var textureStore = new FS.Store.GridFS("textures");
TextureFiles = new FS.Collection("textures", {
stores: [textureStore]
});
Textures = new Mongo.Collection("textures");
客户端.js
Template.hello.events({
'change #myFileInput1': function(event, template) {
uploadTextureToDb('first',event);
}
});
function uploadTextureToDb(name, event) {
FS.Utility.eachFile(event, function(file) {
TextureFiles.insert(file, function (err, fileObj) {
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
console.log('inserted');
console.log(fileObj);
//after file itself is inserted, we also update Texture object with reference to this file
Meteor.call('updateTexture',name,fileObj._id);
});
});
}
服务器.js
Meteor.methods({
updateTexture: function(textureName, fileId) {
Textures.upsert(
{
name:textureName
},
{
$set: {
file: fileId,
updatedAt: Date.now()
}
});
}
});
当您使用autoForm和simpleSchema时,这可能并不那么容易,但是我建议您首先忘记autoForm和simpleSchema,并尝试使其与简单的html和默认集合一起使用。
一切正常后,您可以返回设置它们,但请注意,当涉及到 CollectionFS 时可能会有更多问题,尤其是在涉及由 autoForm 生成的样式时。
相关文章:
- 引用另一个对象javascript中的对象
- JavaScript+引用另一个函数中的对象
- 如何使用 ahref 链接引用另一个页面 ID
- 在打字稿中隐式引用另一个内部模块
- Javascript 此关键字引用另一个对象
- 在CSS中引用另一个元素/在CSS中进行数学运算
- 如何在插入时引用另一个集合
- 如何在高位图表的每个类别中引用另一个系列的yData
- 如何引用另一个对象's在javascript中的属性
- drawImage没有'当我引用另一个javascript文件中的图像时,它不起作用
- 可以在解决方案中重新处理javascript intellisense引用另一个项目中的js文件
- MongoDB-插入到两个集合中,一个集合引用另一个作为子文档
- 在mongo架构上定义一个属性,引用另一个架构和额外的字段
- 引用另一个包含'这'Javascript中的引用
- 在AngularJS中,如何让一个工厂函数引用另一个工厂函数?
- Mongoose -使用require引用另一个模型
- 更新引用另一个模型而不重复的属性
- 是否可以创建一个函数,其中[[prototype]]引用另一个函数
- 如何在一个页面上引用另一个页面中的事件上的类
- 在引用另一个模式的MongoDB模式上创建一个文本索引