Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know the API (mailchimp) you're using, so the solution might be totally wrong. But generally you can see the difference between synchronous and asynchronous programming. Please read some tutorials to fully understand the Node.js way, because it takes some time to embrace this kind of programming :-)</p> <p>The most important thing is that functions typically don't return values, but call a callback-function (sometimes not named <code>callback</code> but <code>done</code>) when they are ready or an error was encountered.</p> <pre><code>function signUp(name, email, callback) { // &lt;----- provide a callback var MailChimpAPI = require('mailchimp').MailChimpAPI; var API_KEY = 'asdfasdf', LIST_ID = 'asdfasdfas'; var api = new MailChimpAPI(API_KEY, { version: '1.3', secure: false }); var names = name.split(' '), status; var data = { apikey: API_KEY, id: LIST_ID, email_address: email, merge_vars: {'FNAME': names[0], 'LNAME': names[1]}, double_optin: false, send_welcome: true }; api.listSubscribe(data, function (error, result) { if (error) { console.log(error); callback(error); // &lt;--- this is one of the "exit"-points of the function } else { console.log('success'); callback(null, result); // &lt;--- this is another } }); // &lt;----------- no return! nobody cares about the return value. } exports.send = function (req, res, next) { var name = req.query.name, email = req.query.email; res.writeHead(200, {"Content-Type": "json"}); signUp(name, email, function (err, result) { // this function gets called when signUp is done (maybe seconds later) if (err) { res.send(500); return; } // don't know what the result looks like res.send({status: 'success'}); // res.end(); DON'T USE res.end() in express! }); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload