2016-01-25 10 views
13

Próbuję zrozumieć, jak zrobić zdjęcie (plik używając CollectionFS) i wstawić id obrazka na moje przedmioty imageId murawę:Jak odwołać się do innej kolekcji przy wstawianiu?

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

Teraz moi pozwalają przepisy są:

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

Używam AUTOFORM więc moja postać wygląda następująco:

klient/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> 

W tej chwili, gdy wybiorę przeglądanie i wybierz obraz, będzie on w bazie danych i chcę wziąć to _id ma i umieścić go w Item, który jest tworzony później, ale w jaki sposób pobrać ten konkretny obraz? Pomyślałem, że to dobry sposób na odniesienie się do obrazu.

UPDATE 1

Sprawdzaj identyfikator faktycznie zlokalizowana jest ukryty po wybraniu pliku:

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

Więc próbuję dostać ma633fFpKHYewCRm8 być umieszczony jako String w ImageId.

UPDATE 2

Może jeden z nich jest użycie FS.File Reference?

Odpowiedz

1

Mam rozwiązać ten sam problem raczej prostsze, gdy plik jest włożona, po prostu zadzwoń to metoda, która robi powiązanego aktualizacji kolekcji:

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); 
     }); 
    }); 
    } 
Serwer

.js

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

jak używasz AUTOFORM i simpleSchema, to może nie być tak łatwo, ale proponuję, aby zapomnieć o AUTOFORM i simpleSchema w pierwszym i starać się pracować z prostymi HTML i domyślne kolekcjach.

Po tym, jak wszystko działa, możesz wrócić do konfigurowania tych, ale pamiętaj, że może być więcej problemów, jeśli chodzi o CollectionFS, szczególnie jeśli chodzi o stylizację wygenerowaną przez autoForm.

Powiązane problemy