0

total amateur here with the following code:

  app.post("/", (req, res) => {

  const firstName = req.body.fname;
  const lastName = req.body.sname;
  const email = req.body.email;

  var subscribingUser = {
    firstName: firstName,
    lastName: lastName,
    email: email
  }

  const run = async () => {
    const response = await client.lists.addListMember("0790pipi4a766d", {
      email_address: subscribingUser.email,
      status: "subscribed",
      merge_fields: {
          FNAME: subscribingUser.firstName,
          LNAME: subscribingUser.lastName
      }
    });
    console.log(response.status);
  }

// IF YOU COME BACK, SOMETHING IS NOT RIGHT HERE, THE CONDITIONAL FLOW DOESN'T WORK. FIND THE SOLUTION.
if (response.status === "subscribed") {
  res.send("<h1>Success</h1>");
  run();
} else {
  res.send("<h1>Something went wrong. Give it another go.</h1>");
  run();
}

});

app.listen(port, () => {
  console.log(`Application currently running on port ${port}`);
});

and nothing happened and after a while error says the response is not defined.

Would be very glad if you could refer me to another similar post. I couldn't find one. Thanks!

  • 1
    Well, the attempt shown (1) has a syntax error (unclosed quotes around a string) and (2) isn't integrated with the code shown. Can you update to a more complete example and indicate what debugging observations are made, such as what `response` is observed to contain when logged to the console? – David Jul 26 '22 at 20:48
  • Are you using Express? If so would you please add a tag for that? – Jake Jul 26 '22 at 20:48
  • to clarify the first point of @David's comment: in your second block of code you are missing a `"` after `"subscribed` in the first line. – Jake Jul 26 '22 at 20:54
  • You're also missing the `}` at the end of the `else` block. But if you fix these typos, you should be able to put that where you currently have `console.log(response.status)` and it should work. – Barmar Jul 26 '22 at 20:57
  • Guys, thank you for the responses. Dave, the response come from the api request I make to mailChimp, it contains a lot of stuff in there but I am pretty sure I choose the correct key to access, what is confusing is that there is a key of statusCode, but I couldn't catch it. However, I tried again the conditional outside the scope and it says response is not defined, means the variabel is not global? And Jake, thank you for the advise, I just did that again, hope could get some more help related to my issue. Barmar, I confirm it is a typo, thanks man. – Rachmat Fadlin Jul 26 '22 at 21:47
  • @RachmatFadlin: The `response` variable is defined and only exists within the `run` function. It can't be used outside that function. The attempt to use it doesn't really make much sense either... You're trying to use the result of an operation within the `run` function to determine whether or not to even execute the `run` function? How can something that's calculated *in the function* be used *before the function is invoked*? – David Jul 27 '22 at 03:09
  • @David, I also thought about that, it was just a blindly effort of try and error and so far couldn't make it works. For all of you guys, thank you so much for the feedback and especially the notification about the similar thread. Will spend some time there to understand it. Take care and have a good day. – Rachmat Fadlin Jul 27 '22 at 16:48

0 Answers0