0

Could you help me with the error in my code. I am trying to connect Mailchimp to my app, so that when a user subscribes with an email, it is directly added to "audience" in Mailchimp. I have written the code, but the code is complaining about "options" object.

import { request } from "http";
import { resolve } from "url";
import { response } from "express";
import { reject } from "lodash";

export async function keepSubscribers(email: string) {
  const data = {
    members: [
      {
        // eslint-disable-next-line @typescript-eslint/camelcase
        email_address: email,
        status: 'subscribed'
      }
    ]
  }
  const postData = JSON.stringify(data)

  const options = {
    url: 'https://us20.api.mailchimp.com/3.0/lists/b0404295b1',
    method: 'POST',
    headers: {
      Authorization: 'auth 71634e399a09a918610fd25094e6731c-us20'
    },
    body: postData
  }

  return new Promise((resolve, reject) => {
    request(options: Object, (err: Object, response: { statusCode: number }) => {
      if (err) {
        return reject(`Request cannot be processed`);
      } else {
        if (response.statusCode === 200) {
          return resolve(true)
        } else {
          return reject(`Request cannot be processed`);
        }
      }
    })
  })
}

enter image description here

NZMAI
  • 536
  • 5
  • 27
  • what is the problem here? can you provide more specific details like error details or what are received vs expected? And did you installed types definitions for `request`? – Salitha Oct 02 '19 at 18:04

1 Answers1

0

Try removing the type in the call to request.

That is, replace this line:

    request(options: Object, (err: Object, response: { statusCode: number }) => {

with this:

    request(options, (err: Object, response: { statusCode: number }) => {

Since options is a value passed as parameter in that line (no need to declare it there).

If the problem persists after this change please specify some other details about it.

Daniel Duarte
  • 464
  • 4
  • 12