如何在插入时引用另一个集合?

13

我正在尝试弄清如何将图像(使用CollectionFS的文件)插入到我的物品imageId字段中:

lib/collections/items.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
  }
}));

lib/collections/images.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/*']
      },
    }
  });
}

目前我的允许规则如下:

服务器/ allows.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;},
});

我正在使用Autoform,所以我的表单看起来像这样:

客户端/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作为一个String放置在ImageId中。

更新2

也许一种方法是使用FS.File引用

1个回答

1

我已经找到了更简单的解决方案。在文件插入后,我只需调用一个方法来执行相关的集合更新:

client.html

<template name="hello">
<p>upload file for first texture:   <input id="myFileInput1" type="file"> </p>
</template>

lib.js

var textureStore = new FS.Store.GridFS("textures");

TextureFiles = new FS.Collection("textures", {
  stores: [textureStore]
});

Textures = new Mongo.Collection("textures");

client.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);
      });
    });
  }

server.js

  Meteor.methods({
    updateTexture: function(textureName, fileId) {
      Textures.upsert(
        {
          name:textureName
        },
        {
          $set: {
            file: fileId,
            updatedAt: Date.now()
          }
        });
    }
  });

由于您正在使用autoForm和simpleSchema,可能不太容易,但是我建议您首先忘记autoForm和simpleSchema,并尝试使用简单的HTML和默认集合来使其正常工作。
一切都正常之后,您可以回到设置它们,但请注意,当涉及到CollectionFS时,可能会有更多问题,特别是在自动表单生成的样式方面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接