javascript - How to reference another collection on insert? -


i'm trying figure how take image (a file using collectionfs) , insert image's id items imageid field:

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 client create generic fs store don't have // access (or want access) 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/*']       },     }   }); } 

right allow rules are:

server/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;}, }); 

i'm using autoform form looks this:

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> 

right when select browse , select image in database , want take _id has , place in item created afterwards, how fetch particular image? figured way reference image.

update 1

find out id located hidden after file selected:

<input type="hidden" class="js-value" data-schema-key="image" value="ma633ffpkhyewcrm8"> 

so i'm trying ma633ffpkhyewcrm8 placed string in imageid.

update 2

maybe 1 way use fs.file reference?

i have solved same problem rather simpler, after file inserted, call method related collection update:

client.html

<template name="hello"> <p>upload file 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 id fileobj._id, , kicked off data upload using http         console.log('inserted');         console.log(fileobj);         //after file inserted, update texture object reference 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()           }         });     }   }); 

as using autoform , simpleschema, might not easy, suggest forget autoform , simpleschema @ first , try make work simple html , default collections.

after works, can go setting those, beware there might more issues when comes collectionfs, when comes styling generated autoform.


Comments