类型{....}的参数不能分配给'string |类型的参数。网址”

时间:2019-10-02 05:37:47

标签: node.js mailchimp loopback4

您能帮我解决代码中的错误吗?我试图将Mailchimp连接到我的应用程序,以便当用户订阅电子邮件时,将其直接添加到Mailchimp中的“受众群体”。我已经编写了代码,但是代码抱怨“选项”对象。

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

1 个答案:

答案 0 :(得分:0)

尝试删除对request的调用中的类型。

也就是说,替换此行:

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

与此:

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

由于选项是在该行中作为参数传递的值(无需在那里声明)。

如果此更改后问题仍然存在,请指定有关此问题的其他详细信息。