2016-01-25 4 views
13

Ich versuche, wie Abbildung eines Bildes nehmen (eine Datei mit CollectionFS) und legen Sie die ID des Bild in meinem Artikel imageId Feld:Wie wird beim Einfügen auf eine andere Sammlung verwiesen?

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

Gerade jetzt meine erlauben Regeln sind:

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

Ich bin mit Autoform so meine Form wie folgt aussieht:

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> 

Gerade jetzt, wenn ich browse und wählen Sie ein Bild aus wird es in der Datenbank sein und ich möchte, dass _id es hat und legen Sie in der Item, die danach erstellt wird, aber wie hole ich dieses bestimmte Bild? Ich dachte, das ist ein guter Weg, ein Bild zu referenzieren.

UPDATE 1

die ID finden Sie versteckt tatsächlich befindet, nachdem eine Datei ausgewählt ist:

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

So versuche ich ma633fFpKHYewCRm8 zu bekommen als String platziert werden in die ImageId.

UPDATE 2

Vielleicht ein Weg ist, FS.File Reference zu benutzen?

Antwort

1

ich das gleiche Problem gelöst haben eher einfacher, nachdem Datei eingefügt wird, nenne ich nur eine Methode, die die zugehörige Sammlung Update enthält:

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

wie Sie Autoform und simpleSchema verwenden, ist es vielleicht nicht so einfach sein, aber ich schlage vor, Sie über Autoform und simpleSchema zunächst zu vergessen und versuchen, es mit einfachen HTML-und Standardsammlungen zu arbeiten.

Nachdem alles funktioniert, können Sie die Einrichtung wieder aufnehmen. Beachten Sie jedoch, dass es bei CollectionFS weitere Probleme geben kann, insbesondere wenn es um das von autoForm erstellte Styling geht.

Verwandte Themen