Configure Mandrill with Meteor fail - javascript

I always get error saying:
TypeError: Cannot call method 'config' of undefined.
This is my startup function on server.js. What do I do wrong?
Meteor.startup(function() {
return Meteor.Mandrill.config({
username: "SMTP Username",
key: "Valid API Key",
password: "Valid API Key",
port: "587",
host:"smtp.mandrillapp.com"
});
});

The meteor startup function is not designed to return something, this is your first error.
On an other hand, I can see on their documentation that you have to configure the object Mandrill directly.
if (Meteor.isServer) {
// server code
Mandrill.config({
username: "SMTP Username",
key: "Valid API Key",
password: "Valid API Key",
port: "587",
host:"smtp.mandrillapp.com"
// baseUrl: 'https://mandrillapp.com/api/1.0/' // update this in case Mandrill changes its API endpoint URL or version
});
// you can put a Meteor startup here if you want :
Meteor.startup(function() {
// Do somthing else, like populating, ...
});
}

Related

How to get a username from response object using Auth0 Lock?

The question is how to get username, which I used to login, back from response object?
I'm creating the Auth0Lock instance by following code:
this._lock = new Auth0Lock(AUTH_CONFIG.clientId, AUTH_CONFIG.domain, AUTH_CONFIG.options);
and then I subscribe on "authenticated" event:
this._lock.on('authenticated', authResult => {
this._lock.getUserInfo(authResult.accessToken, function(error, profile) {
console.log('profile', profile); // --> undefined
if (error) {
// Handle error
}
});
})
I'm logging in by following credentials:
username: john#gmail.com password: 123456
I want to be able to see 'username: john#gmail.com' somewhere in authResult object.
But unfortunately I don't see.
Should I add something in Auth0lock options?
P.S. I've added following code inside the handler of "authenticated" event, but it returns undefined for profile.
I've just added scope: 'openid' into "auth" property of options
options: {
...
auth: {
...
scope: 'openid' <---
}
}

How can I send email notifications with Parse and Mandrill?

I am trying to use Mandrill to send an event-based email notification to the users of my web app. I am using Parse with Back4App.
In this tutorial (https://docs.back4app.com/docs/integrations/parse-server-mandrill/), the hosting providers suggest using the following method to call the Mandrill cloud code from an Android application:
public class Mandrill extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("your back4app app id”)
.clientKey(“your back4app client key ")
.server("https://parseapi.back4app.com/").build()
);
Map < String, String > params = new HashMap < > ();
params.put("text", "Sample mail body");
params.put("subject", "Test Parse Push");
params.put("fromEmail", "someone#example.com");
params.put("fromName", "Source User");
params.put("toEmail", "other#example.com");
params.put("toName", "Target user");
params.put("replyTo", "reply-to#example.com");
ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback < Object > () {
#Override
public void done(Object response, ParseException exc) {
Log.e("cloud code example", "response: " + response);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mandrill);
}
}
How can I implement this in JavaScript with the Parse JavaScript SDK?
This is what I've done so far but it won't send an email. I have Mandrill set up, as well as a verified email domain and valid DKIM and SPF.
// Run email Cloud code
Parse.Cloud.run("sendMail", {
text: "Email Test",
subject: "Email Test",
fromEmail: "no-reply#test.ca",
fromName: "TEST",
toEmail: "test#gmail.com",
toName: "test",
replyTo: "no-reply#test.ca"
}).then(function(result) {
// make sure to set the email sent flag on the object
console.log("result :" + JSON.stringify(result));
}, function(error) {
// error
});
I don't even get a result in the console, so I figure the cloud code is not even executing.
You have to add the Mandrill Email Adapter to the initialisation of your Parse Server, as described on their Github page. Also check the Parse Server Guide for how to initialise or use their example project.
Then set up Cloud Code by following the guide. You'll want to either call a Cloud Code function using your Android app or from any Javascript app, or use beforeSave or afterSave hooks of a Parse Object directly in Cloud Code, which allow you to send Welcome Emails when a user signs up. That could come in handy if you want to implement behaviour based emails based on object updates. Plus, because it is on the server and not the client, it is easier to maintain and scale.
To make the Cloud Code function actually send an email via Mandrill, you need to add some more code to your Cloud Code function. First, add a file with these contents:
var _apiUrl = 'mandrillapp.com/api/1.0';
var _apiKey = process.env.MANDRILL_API_KEY || '';
exports.initialize = function(apiKey) {
_apiKey = apiKey;
};
exports.sendTemplate = function(request, response) {
request.key = _apiKey;
return Parse.Cloud.httpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: 'https://' + _apiUrl + '/messages/send-template.json',
body: request,
success: function(httpResponse) {
if (response) {
response.success(httpResponse);
}
return Parse.Promise.resolve(httpResponse);
},
error: function(httpResponse) {
if (response) {
response.error(httpResponse);
}
return Parse.Promise.reject(httpResponse);
}
});
};
Require that file in your Cloud Code file, and use it like any other Promise.
var Mandrill = require("./file");
Mandrill.sendTemplate({
template_name: "TEMPLATE_NAME",
template_content: [{}],
key: process.env.MANDRILL_API_KEY,
message: {
global_merge_vars: [{
name: "REPLACABLE_CONTENT_NAME",
content: "YOUR_CONTENT",
}],
subject: "SUBJECT",
from_email: "YOUR#EMAIL.COM",
from_name: "YOUR NAME",
to: [{
email: "RECIPIENT#EMAIL.COM",
name: "RECIPIENT NAME"
}],
important: true
},
async: false
})
.then(
function success() {
})
.catch(
function error(error) {
});
Make sure you create a template on Mailchimp, right click it and choose "Send to Mandrill", so that you can use that template's name when sending via the API.
It's a bit involved, but once set up, it works like a charm. Good luck!

Parse-server social login

I am developing application based on Parse-server and I want to offer social login. I found this guide in the documentation http://docs.parseplatform.org/js/guide/#linking-users.
I started to implement the social login by google. I did following steps:
1) I added following lines to the ParseServer settings
var api = new ParseServer({
...
auth:{
google: {}
},
...
});
2) I did the authentication by hello.js on the client side (call user._linkWith function on login)
hello.init({
google: 'My Google id'
});
hello.on('auth.login', function(auth) {
// Call user information, for the given network
hello(auth.network).api('me').then(function(r) {
const user = new Parse.User();
user._linkWith(auth.network, auth.authResponse).then(function(user){
console.log('You are logged in successfully.');
});
});
});
When I debugged it, I found that it fails in _linkWith() function, when provider object is preparing. Object AuthProviders, which should store all providers, is empty. Because of it the statement provider = authProviders['google']; leads to undefined. Invoking provider.authenticate(...); leads to error "Cannot read property 'authenticate' of undefined"
What am I missing or what am I doing wrong?
Thanks for all your answers.
Honza
Did you register the authenticationProvider? You can find examples in our unit tests on how to do so:
https://github.com/parse-community/parse-server/blob/5813fd0bf8350a97d529e5e608e7620b2b65fd0c/spec/AuthenticationAdapters.spec.js#L139
I also got this error and looked at the _linkWith(provider, options) source code. It checks if options has an authData field (which in turn should contain id and credentials). If so, it uses options.authData. Otherwise it falls back on looking up a previously registered authentication provider mentioned in the previous answer.
This is a fragment of the code I'm using:
const authData = {
"id": profile.getId(),
"id_token": id_token
}
const options = {
"authData": authData
}
const user = new Parse.User();
user._linkWith('google', options).then(function(user) {
console.log('Successful user._linkWith(). returned user=' + JSON.stringify(user))
}, function(error) {
console.log('Error linking/creating user: ' + error)
alert('Error linking/creating user: ' + error)
// TODO handle error
})

How Do i connect sails to mongodb?

Im new to sails Js and mongodb. I am absolutely a newbie.
Problem:
I already have a user collection on my mongodb database. I want to connect it to sails to display the list of collection.
Ive seen sails js documentations. I already installed sails-mongo adapter and edited the connection.js....
module.exports = {
// this is my model
attributes: {
firstname: {
type: 'string'
}
},
findUser: function(opts,callback){
///// How am I going to connect to mongo and query the users ?
//// user.find('John Doe') wont work here.
}
}
Run: npm install sails-mongo
in config/connections.js uncomment section related to mongo and enter necessary info there
someMongodbServer: {
adapter: 'sails-mongo',
host: 'mongohost.com',
port: 55915,
user: 'user',
password: 'password',
database: 'dbname'
},
in config/models.js enter you connection adapter variable
...
connection: 'someMongodbServer',
...
done
EDIT:
In order to get the data from database use the following:
User.find({firstname: 'John Doe'}).exec(function(error, user) {
console.log(user);
});

Email.send issue is email is not defined in Meteor

I need to send email by using Meteor. I did the code to regarding send mail. I have added a package email. But I got an error. I have no idea what happening. Check out the error & code below.
Error :
=> Meteor server running on: http://localhost:3000/
I20140118-10:54:35.900(5.5)? Exception while invoking method 'sendEmail' Referen
ceError: email is not defined
I20140118-10:54:35.989(5.5)? at Meteor.methods.sendEmail (app/loginapp.js:13
7:39)
I20140118-10:54:35.989(5.5)? at maybeAuditArgumentChecks (packages/livedata/
livedata_server.js:1349)
I20140118-10:54:35.990(5.5)? at packages/livedata/livedata_server.js:569
I20140118-10:54:35.990(5.5)? at _.extend.withValue (packages/meteor/dynamics
_nodejs.js:35)
I20140118-10:54:35.990(5.5)? at packages/livedata/livedata_server.js:568
I20140118-10:54:35.992(5.5)? at _.extend.withValue (packages/meteor/dynamics
_nodejs.js:35)
I20140118-10:54:35.992(5.5)? at _.extend.protocol_handlers.method (packages/
livedata/livedata_server.js:567)
I20140118-10:54:35.992(5.5)? at packages/livedata/livedata_server.js:472
JS Code :
if (Meteor.isClient)
{
Template.hello.greeting = function ()
{
return "Welcome to email.";
};
Template.hello.events
({
'click input' : function ()
{
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed the button");
// In your client code: asynchronously send an email
Meteor.call('sendEmail',
'*****#gmail.com',
'****#gmail.com',
'Hello from Meteor!',
'This is a test of Email.send.');
}
});
}
if (Meteor.isServer)
{
Meteor.startup(function ()
{
// code to run on server at startup
process.env.MAIL_URL = 'smtp://****#gmail.com:**password**#smtp.sendgrid.net:587';
});
Meteor.methods
({
sendEmail: function (to, from, subject, text)
{
console.log("*** sendEmail ***");
// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();
Email.send
({
to: to,
from: from,
subject: subject,
text: text
});
}
});
}
i think you need to have email package installed
meteor add email

Categories

Resources