Created
August 3, 2017 20:12
-
-
Save TalhaAwan/5e2d5ec9009e7a86bf6773222fdc2d64 to your computer and use it in GitHub Desktop.
Node/Express endpoint to send post cards to multiple users (lob.com service)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Router = require('express').Router; | |
const router = new Router(); | |
const Lob = require('lob')('test_3168a75ff1d5412e93168ff1de3747a1523'); //"lob": "^3.5.0", | |
const async = require('async'); | |
router.post('/post-card', function(req, res){ | |
const users = req.body.users; | |
async.mapLimit(users, 5, function(user, callback) { | |
var to = { | |
name: user.name, | |
company: user.company, | |
address_line1: user.addressLine1, | |
address_city: user.addressCity, | |
address_state: user.addressState, | |
address_zip: user.addressZip | |
} | |
Lob.postcards.create({ | |
description: 'Demo Postcard job', | |
to: to, | |
front: '<html> <body> ... </body> </html>', | |
back: '<html> <body> ... </body> </html>', | |
data: { | |
name: user.name, | |
address: user.addressLine1 + " " + user.addressCity + " " + user.addressState + " " + user.addressZip; | |
} | |
}, function (err, result) { | |
if(err){ | |
return callback(err); | |
} | |
else{ | |
return callback(null, result) | |
} | |
}); | |
}, function(err, resultSet){ | |
if(err){ | |
return res.status(500).json(err); | |
} | |
else{ | |
return res.json(resultSet); | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment