In meteor is it possible to click on a button on the client and then have that button output a files contents from the server?
The reason for this is based around documentation. I'd like to create a button that can copy and paste the html from a template file.
On the server side I'd need to read the file and then somehow pass that to the client to output. Is this possible?
Something like below should do the job:
Server side:
Meteor.methods({
loadFile:function(path){
var fs = Npm.require('fs');
return fs.readFileSync(path, 'utf8');
}
})
Client side:
Template.NAME.created = function(){
this.file = new ReactiveVar("");
}
Template.NAME.helpers({
file:function(){
return Template.instance().file.get()
}
})
Template.NAME.events({
'click button':function(e,t){
Meteor.call('loadFile','public/file.html', function(err, result) {
if(!err){
t.file.set(result)
}else{
console.error('Error', err)
}
})
}
})
Related
I'm using accounts.ui for forget password
Accounts.forgotPassword({email: "test#test.com"}, function (e, r) {
if (e) {
console.log(e.reason);
} else {
// success
}
});
When I send the email for forget password I simply get below email
Can someone tell me how to simply change the url and send the token with the same token
I tried using below code but that didn't work
main.js(client)
Meteor.startup(() => {
Accounts.resetPassword.text = function(user, url) {
url = url.replace('#/', '/');
console.log("url ==== ", url)
return `Click this link to reset your password: ${url}`;
}
});
On the server side:
Accounts.emailTemplates.resetPassword.subject = function () {
return "Forgot your password?";
};
Accounts.emailTemplates.resetPassword.text = function (user, url) {
return "Click this link to reset your password:\n\n" + url;
};
Read: https://docs.meteor.com/api/passwords.html#Accounts-emailTemplates
To change the resetPassword url to a custom one you have to run below code on your server (inside of /server/main.js file).
Accounts.urls.resetPassword = function (token) {
return FlowRouter.url("/reset-password/:token/", { token });
};
In this case, I am using a FlowRouter to generate that URL but you could technically just return a template literal if you like.
If the above code is in the server main.js file and you run Accounts.forgotPassword() function on the frontend from a localhost, it would generate this link:
http://localhost:3000/reset-password/C9cGfgaLEgGXbCVYJcCLnDYiRi3XJpmt2irLOOaKe56
I am facing problems while creating a PDF from HTML in Meteor 1.4.4.2 and 1.5 which is latest version till date.
I want to know how to create a PDF from HTML at server side?
Below are the steps to generate PDF from HTML Template using Meteor 1.4.4.2
Kindly goto PROJECT/.meteor/packages file and add packages as below
meteorhacks:ssr
bryanmorgan:webshot
Make sure whichever html page you want to convert to pdf must be present at location PROJECT/private as per Meteor 1.4.4.2 guidelines. In this example, the file name is app_clientagreement.html (file is simple html).
We will now create a method at server side to generate PDF. The file is at location PROJECT\imports\startup\server\methods.js (make sure you import the method.js file inside PROJECT\imports\startup\server\index.js as import './methods.js'). Inside methods.js write below code
import { Meteor } from 'meteor/meteor';
import { SSR, Template } from 'meteor/meteorhacks:ssr';
Meteor.methods({
'generate_pdf'(){
var fs = Npm.require('fs');
var Future = Npm.require('fibers/future');
var fut = new Future();
var fileName = "pokemon-report.pdf";
SSR.compileTemplate('agreement', Assets.getText('app_clientagreement.html'));
var html_string = SSR.render('agreement');
var options = {
"paperSize": {
"format": "Letter",
"orientation": "portrait",
"margin": "1cm"
},
siteType: 'html'
};
webshot(html_string, fileName, options, function(err) {
fs.readFile(fileName, function (err, data) {
if (err) {
return console.log(err);
}
fs.unlinkSync(fileName);
fut.return(data);
});
});
let pdfData = fut.wait();
let base64String = new Buffer(pdfData).toString('base64');
return base64String;
}
});
To call this method in Template, for ease to get auto-called, I am calling it when a template is rendered. The code is below,
Template.App_Agreement.onRendered(function(){
Meteor.call('generate_pdf', function(err, res) {
if (err) {
console.error(err);
} else if (res) {
window.open("data:application/pdf;base64, " + res);
}
});
});
When you call above template (which is 'App_Agreement'), a new tab is opened and a PDF is rendered.
I want to show some data points on the client side, and I want to get the latitude, longitude and summary from a file stored on the server.
I have read a lot of posts saying to use papaParse using Meteor methods but I am not able to make it work.
Can you guys point me to right direction, my questions are:
In which folder to should I store a .txt, .csv or .json file in Meteor?
How to access it from the client and return the read data to client for display.
You can put your static files into private folder on the server and get them through Assets.
For exmaple, you have a data.json file in your private folder.
Method to get this data:
Meteor.methods({
getData() {
return JSON.parse(Assets.getText('data.json'));
}
});
You can now call this method on the client:
Meteor.call('getData', function(err, res) {
console.log(res);
});
UPD
Ok, how to display it.
Meteor.call runs async, so we will use reactivity to update our view on result.
Here is how we can display data on ourData template.
<template name="ourData">
<!-- Here you may want to use #each or whatever -->
<p>{{ourData}}</p>
</template>
Template.ourData.onCreated(function() {
this.ourData = new ReactiveVar();
Meteor.call('getData', (err, res) => {
if (err) {
console.error(err);
} else {
// Putting data in reactive var
this.ourData.set(res);
}
});
});
Template.ourData.helpers({
ourData: function() {
// Helper will automatically rerun on method res
return Template.instance().ourData.get();
}
});
reactive-var package required or you can also use Session.
i am very new to nodejs and meteor. i need to create a page content-type application/json and data from mongo collection. So when collection data change json page must be change.
For json page i use this example:
https://stackoverflow.com/a/23666992/1446182
if (Meteor.isServer) {
Meteor.startup(function () {
try{
var interval = Meteor.setInterval(function() {
var result = Meteor.http.call("GET", "http://192.168.2.144//ihale/meteorGetPage" );
var resultJson = JSON.parse(result.content);
var json = IhaleCollection.findOne();
IhaleCollection.update(json, {$set: {json: resultJson}});
}, 1000);
}
catch(err) {
console.log(err);
}
});
Router.map(function() {
this.route('jsonExample', {
where: 'server',
path: '/json',
action: function() {
var obj = IhaleCollection.findOne();
var headers = {'Content-type': 'application/json'};
this.response.writeHead(200, headers);
this.response.end(JSON.stringify(obj));
}
});
});
}
When Meteor.startup i start to update IhaleCollection every second.
So how i can update json page when IhaleCollection change?
You can't. Not this way anyway. JSON has no builtin mechanism to detect if the source has changed and given that the router endpoint is outputting raw JSON data with no javascript, no automatic updates will happen. Only when a client refreshes the endpoint will there be an update.
Meteor deals with reactive data by inserting javascript in the underlying html page. If you want to utilize that then you have to write a template, write a helper method to return JSON and use the helper method in the body.
The helper method should look something like this:
Template.jsondoc.helpers{(
json: function(){
return JSON.stringify(IhaleCollection.findOne());
}
})
Template can be as simple as that:
<template name="jsondoc">
{{json}}
</template>
and your route will be as simple as this:
Router.route('/jsondoc');
I am very new to Meteor so if my apologies if this is a dumb question:
How do you read a file from the filesystem on the server side, and get those contents displayed on the client side. Doing it synchonously isn't a problem because I can return it right away but as I understand from reading that defeats the purpose of Meteor so I am trying to do it asynchronously. However I dont know how to connect the client/server in this fashion. Publish/Subscribe seem to only be for db collections and not filesystem files. Any guidance you can give me would be very much appreciated.
if (Meteor.isClient) {
Template.body.helpers({
myfile: function(){
return file_contents;
}
});
}
if (Meteor.isServer) {
Meteor.methods({
myfile: function(){
var fs = Npm.require("fs");
fs.readFile('/opt/SI/SIAgent/conf/myfile','utf-8', function (err, data) {
file_contents = data;
});
}
});
}
You have to use Meteor.wrapAsync to turn Node.JS fs.readFile into synchronous-looking code so that you can return the result within the Meteor method.
var fs=Npm.require("fs");
fsReadFileSync=Meteor.wrapAsync(fs.readFile,fs);
Meteor.methods({
getMyFileContent:function(){
return fsReadFileSync("/opt/SI/SIAgent/conf/myfile","utf-8");
}
});