How to access FS collections metadata in meteor - meteor

My question is solved:
It missed this line of code before the insert code:
var fileObj = new FS.File(file);
How to access FS collections metadata in meteor?
I tried: var result = Images.find({"metadata.gallery._id":curgallery})
(curgallery is the _id of a gallery object)
Here is my code:
helper:
images: function () {
if (Session.get('gallery')) {
var curgallery = Session.get('gallery')._id;
var result = Images.find({"metadata.gallery._id":curgallery})
console.log(result.fetch());//return an empty array
return result;
};
events:
//INSERT IMAGE
'change .fileInput': function(event, template) {
if (Session.get('gallery')) {
var collection = Session.get('gallery');
FS.Utility.eachFile(event, function(file) {
file.metadata = {owner: Meteor.user()._id,gallery:collection};
Images.insert(file, function (err, fileObj) { } );
console.log(file.metadata.gallery._id);//return the _id's gallery
});
};
},

Related

Create a universal helper variable

Is there a way to create a variable at the top of template helpers to remove duplication.
In this particular situation I'm using var candidate = FlowRouter.getParam('id'); and I have to create the variable in each helper. I assume there is a better way.
professionalOverview: function() {
var candidate = FlowRouter.getParam('id');
return ProfessionalOverview.findOne({ candidateUserId: candidate });
},
candidateImg: function() {
var candidateUserId = FlowRouter.getParam('id');
return Files.findOne({ userId: candidateUserId });
},
EDIT
Template.talentProfileNew.onCreated(function() {
var self = this;
self.autorun(function(){
this.candidateUserId = new ReactiveVar(FlowRouter.getParam('id'));
}
});
Template.talentProfileNew.helpers({
candidate: function() {
console.log(Template.instance().candidateUserId.get());
return Meteor.users.findOne({_id: Template.instance().candidateUserId.get()});
}
});
you could read it once in onCreated() and put it in a reactive var. e.g.
Template.Foo.onCreated(function() {
this.candidateUserId = new ReactiveVar(FlowRouter.getParam('id'));
});
Template.Foo.helpers({
candidateImg() {
return ProfessionalOverview.findOne({ userId: Template.instance().candidateUserId.get()});
}
});

Get image dimensions (or metadata) at upload

I'm trying to get the original dimensions for an image while uploading it to a database. Actually it would be great to get all of it's original metadata (XMP, Adobe). But even getting the dimensions is not working:
Template.pixUpload.events({
'change .myPixInput': function(event, template) {
FS.Utility.eachFile(event, function(file) {
// get the image's width
var img = event.target.files[0]
var imgwidth = img.width;
console.log('width: ' + width);
var newFile = new FS.File(file);
newFile.metadata = {width: imgwidth};
MyPix.insert(newFile, function (err, fileObj) {
//If !err, we have inserted new doc with ID fileObj._id, and
//kicked off the data upload using HTTP
});
});
}
});
I use Imagemagick to get all kinds of metadata (like EXIF) from my images.
var assetStore = new FS.Store.GridFS("assetFiles", {
transformWrite: function(fileObj, readStream, writeStream) {
readStream.pipe(writeStream);
// write the image data to the fileobj
getBinaryData(readStream, FS.Utility.safeCallback(function(err, binary) {
var imageData = Imagemagick.identify({
data: binary
});
fileObj.update({
$push: {
data: imageData
}
});
}));
}
});
getBinaryData is a async function that returns the binary data of my image.
I use a package called classcraft:imagemagick since the graphicsmagick package does not give you as much metadata as imagemagick
This works! – copy/modified from a discussion with Sanjo at GitHub. Only problem is I don't fully understand what's happening. Can anyone help me out?
var OriginalsStore = new FS.Store.FileSystem("OriginalPix", {
path: pathToOriginalsFolder,
transformWrite: function (fileObj, readStream, writeStream) {
// write original image to writeStream, no transformations
readStream.pipe(writeStream);
gm(readStream, fileObj.name())
.size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
if (err) {
// handle the error
} else {
fileObj.update({$set: {'metadata.width': size.width, 'metadata.height': size.height}});
}
}));
}
});
This is what are you looking for?
A normal image don't come with the 'width; field, it comes with type,name,dateMod,dateUp,and size.(well atleast my files)
Template.pixUpload.events({
'change .myPixInput': function(event, template) {
FS.Utility.eachFile(event, function(file) {
// get the image's width
var img = event.target.files[0]
var imgwidth = img.width;
console.log('width: ' + width);
console.log(img.lastModified);
console.log(img.lastModifiedDate);
console.log(img.name);
console.log(img.size);
console.log(img.type);
var newFile = new FS.File(file);
newFile.metadata = {
width: imgwidth
name:img.name,
size:img.size,
type:img.type,
lstModDate:img.lastModifiedDate
lstDate:img.lastModified
};
MyPix.insert(newFile, function (err, fileObj) {
//If !err, we have inserted new doc with ID fileObj._id, and
//kicked off the data upload using HTTP
});
});
}
});
Test it

Meteor define ReactiveVar to be accesible in .events and .helpers

I am trying to define a new ReactiveVar variable to be accessible in all the template sections (ex. .events, .helpers, .rendered ...etc) as shown in my code below, yet I am always getting an error:
Error: Exception in template helper:
ReferenceError: logData is not defined
Can someone please tell me what I am missing / doing wrong here? Thanks
Code:
Template.detailedreport.rendered = function() {
var logData = new ReactiveVar;
logData.set([]);
};
Template.detailedreport.helpers({
myCollection: function () {
return logData.get();
}
});
Template.detailedreport.events({
'submit form': function(e) {
e.preventDefault();
var now = Session.get("startDate");
var then = Session.get("endDate");
var custID = Session.get("customer");
var projID = Session.get("project");
Meteor.call('logSummary', now, then, projID, custID, function(error, data){
if(error)
return alert(error.reason);
logData.set(data);
});
}
});
You need to define the ReactiveVar on the template instance like this :
Template.detailedreport.created = function() {
this.logData = new ReactiveVar([]);
};
Then you'll be able to access it in helpers like this :
Template.detailedreport.helpers({
myCollection: function () {
return Template.instance().logData.get();
}
});
In events you can use the template argument :
Template.detailedreport.events({
'submit form': function(e, template) {
e.preventDefault();
var now = Session.get("startDate");
var then = Session.get("endDate");
var custID = Session.get("customer");
var projID = Session.get("project");
Meteor.call('logSummary', now, then, projID, custID, function(error, data){
if(error){
return alert(error.reason);
}
template.logData.set(data);
});
}
});

Meteor data not inserting or displaying after removing insecure package

Data is not getting inserted into the database after i removed autopublish and insecure packages. Please let me know what i am missing.
Userdata = new Meteor.Collection("Userdata");
if (Meteor.isClient) {
Template.sample.events({
"click button.clickeve": function (){
var e_value = $('input[name = "exampleInputEmail1"]').val();
var e_name = $('input[name = "exampleInputName"]').val();
doc = {user_id: Meteor.userId(), e_value:e_value, e_name:e_name}
}
});
Template.temp.list_item = function(){
return Userdata.find();
}
Meteor.subscribe("Userdata");
}
if (Meteor.isServer) {
Meteor.publish("Userdata", function() {
return Userdata.find();
});
Userdata.allow({
insert: function(userID,doc) {
return userID === doc.user_id;
}
});
}
You don't have an insert statement.
Userdata = new Meteor.Collection("Userdata");
if (Meteor.isServer) {
Meteor.publish("Userdata", function() {
return Userdata.find();
});
Userdata.allow({
insert: function(userID,doc) {
return userID === doc.user_id;
}
});
}
if (Meteor.isClient) {
Template.sample.events({
"click button.clickeve": function (e){
e.preventDefault(); // to prevent default action of the button
var e_value = $('input[name = "exampleInputEmail1"]').val();
var e_name = $('input[name = "exampleInputName"]').val();
doc = {user_id: Meteor.userId(), e_value:e_value, e_name:e_name};
Userdata.insert(doc); // actually inserting the document
}
});
Template.temp.list_item = function(){
return Userdata.find();
}
Meteor.subscribe("Userdata");
}

Meteor Insert invisibly and silently hanging

The following code does not update the database everytime a tweet is found - it silently hangs, adding no tweets to the database.
If a tweet is manually added to the DB from the JS console in the browser, it shows up just fine, but no tweets are being added to the DB automatically.
Tweets = new Meteor.Collection("tweets");
if (Meteor.isClient) {
Template.kildeer.tweets = function () {
return Tweets.find({});
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
var require = __meteor_bootstrap__.require,
Twit = require('twit')
, T = new Twit({
consumer_key: 'blahblah',
consumer_secret: 'blahblah',
access_token: 'blahblah',
access_token_secret: 'blahblah'
});
var stream = T.stream('statuses/filter', { track: ['bing', 'google', 'microsoft'] })
stream.on('tweet', function (tweerp) {
var id;
console.log(tweerp.text);
id = Tweets.insert({text: tweerp.text, screen_name: tweerp.user.screen_name, profile_image: tweerp.user.profile_image_url});
console.log(id);
});
});
}
In Meteor, Collection.insert must always be called inside of a Fiber() closure.
Fiber(function() {
Tweets.insert({text: tweerp.text, screen_name: tweerp.user.screen_name, profile_image: tweerp.user.profile_image_url});
}).run();

Resources