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

How to reference another collection on insert?

本文关键字:引用 另一个 集合 插入      更新时间:2023-09-26

我正在尝试弄清楚如何拍摄图像(使用 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 生成的样式时。