tag name ejemplo javascript meteor meteor-helper collectionfs

javascript - ejemplo - getelementsbytagname jquery



¿Cómo hacer referencia a otra colección en la inserción? (1)

He resuelto el mismo problema bastante simple, después de insertar el archivo, simplemente llamo a un método que hace la actualización de la colección relacionada:

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

como estás usando autoForm y simpleSchema, puede que no sea tan fácil, pero te sugiero que te olvides de autoForm y simpleSchema al principio y trates de hacerlo funcionar con simples html y colecciones predeterminadas.

Después de que todo funcione, puede volver a configurarlos, pero tenga en cuenta que puede haber más problemas cuando se trata de CollectionFS, especialmente cuando se trata del estilo generado por autoForm.

imageId averiguar cómo tomar una imagen (un archivo que utiliza CollectionFS ) e insertar la Id. De la imagen en el campo imageId mis elementos:

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

En este momento, mis reglas de permiso son:

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

Estoy usando Autoform para que mi formulario se vea así:

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>

Ahora cuando selecciono explorar y selecciono una imagen, estaré en la base de datos y quiero tomar ese _id que tiene y colocarlo en el Item que se crea después, pero ¿cómo puedo obtener esa imagen en particular? Pensé que esta es una buena manera de hacer referencia a una imagen.

ACTUALIZACIÓN 1

Averigüe que la identificación se encuentra oculta después de seleccionar un archivo:

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

Así que estoy intentando que ma633fFpKHYewCRm8 se coloque como una String en ImageId .

ACTUALIZACIÓN 2

Tal vez una forma es usar FS.File Reference ?