Paul Medawar
Posts: 0
Joined: Thu Apr 03, 2014 10:55 am

SendGrid Server Code - Emailing Object in Body of text

Hi, can anyone help me with this?

I've built a server code that is checking membership expiries. I want it to send an email to a single email address, which include the emails of the expiring members in the body, 45 days before a membership is about to expire.

The first part is working great, and returns the emails of the members that are expiring in 45 days as an object.

-------------------------------

var dbId = "thisisthedatabaseid";
var collectionName = "thisisthecollectionname";
var query = [] ;

var dayBefore = "45";
var untilDate = new Date(new Date().getTime() - (dayBefore * 24 * 3600 * 1000) );
console.log("date = " + untilDate);
var untilDateFormated = untilDate.toISOString().replace(/T/gi, " ").replace(/Z/gi, "");

var dayBefore2 = "44";
var untilDate2 = new Date(new Date().getTime() - (dayBefore2 * 24 * 3600 * 1000) );
console.log("date = " + untilDate2);
var untilDateFormated2 = untilDate2.toISOString().replace(/T/gi, " ").replace(/Z/gi, "");

try
{
result = {};
var params = {};

params.criteria = '{ "$and": [{"MembershipExpire": {"$gt": "'+ untilDateFormated +'" }},{"MembershipExpire": {"$lt": "'+ untilDateFormated2 +'" }}] }';

params.proj = {"_id":0,'Email': 1};
query = Collection.query(dbId,collectionName,params) ;

response.success(query);

}

catch (e)
{
response.success("message: " + e.message + "ncode: " + e.code);
}

-------------------------------

The second part is sending the email and also all works, sending an email out with the results from the above. The only issue is that the results from the above code are coming out as

[object Object],[object Object],[object Object],[object Object]

and if i try JSON.stringify(query), the server code fails.

-------------------------------

var sendGridUrl = "https://api.sendgrid.com/api/mail.sen...";

var to = "thisisanemailaddress"

var subject = "Membership Expiring"

var from = "thisisanemailaddress";

// Set SendGrid API Key
var auth = "Bearer " + "*******************************";

var XHRResponse = XHR2.send("POST", sendGridUrl, {
"parameters": {
"to": to,
"subject": subject,

Code: Select all

 "text": "" + query + "", 
 "from": from 

},
"headers" : {
"Authorization": auth
}
});

Apperyio.response.success(XHRResponse.body, "application/json");

-------------------------------

I'm working on it, but if anybody can see anything obviously wrong, id appreciate a bit of direction :)

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

SendGrid Server Code - Emailing Object in Body of text

Hello,

Please print that value to the console and check the trace tab to see it's values:preconsole.log(JSON.stringify(query));/pre

Return to “Issues”