mailjet / mailjet-apiv3-nodejs

[API v3] Official Mailjet API v3 NodeJS wrapper
https://dev.mailjet.com
MIT License
232 stars 67 forks source link

Object not found trying to add contact to contactList #245

Closed yosle closed 11 months ago

yosle commented 1 year ago

I'm using this function to add a contact to a contactList, (reference: https://dev.mailjet.com/email/reference/contacts/subscriptions#v3_post_contactslist_list_ID_managecontact). According to the documentation if the contact does not exist a new one , should be created under the hood. but i keep getting this strange 404 not found error. I checked the contactList Id exist and not sure what's the issue here.

async function addContactToContactList(listId, name, email) {
    try {
        const result = await mailjetClient.post("contactslist", { 'version': 'v3' })
            .id(listId)
            .action("managecontact")
            .request({
                "Name": name || 'New Contact',
                "Properties": {},
                "Action": "addnoforce",
                "Email": email
            })
        console.log(result.body.Data); //Log the result for debugging
        return result.body.Data;
    } catch (error) {
        console.error(error);
        return false

    }
}

complete error:


Error: Unsuccessful: Status Code: "404" Message: "Object not found"
    at e.<anonymous> (D:\YOSLE\pymemarket\node_modules\node-mailjet\mailjet.node.js:2:37964)
    at D:\YOSLE\pymemarket\node_modules\node-mailjet\mailjet.node.js:2:33895
    at Object.throw (D:\YOSLE\pymemarket\node_modules\node-mailjet\mailjet.node.js:2:34000)
    at r (D:\YOSLE\pymemarket\node_modules\node-mailjet\mailjet.node.js:2:32765)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 'ERR_BAD_REQUEST',
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function (anonymous)],
    transformRequest: [ [Function (anonymous)] ],
    transformResponse: [Function: transformResponse],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function] },
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/json',
      'User-Agent': 'mailjet-api-v3-nodejs/6.0.2',
      'Content-Length': 97
    },
    url: 'https://api.mailjet.com/v3/REST/contactslist/10249638/managecontact',
    params: {},
    data: '{"Name":"New Contact","Properties":{},"Action":"addnoforce","Email":"yosle007+figarog@gmail.com"}',
    method: 'post',
    responseType: 'json',
    auth: {
      username: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    }
  },
  response: {
    status: 404,
    statusText: 'Object not found',
    headers: {
      date: 'Wed, 31 May 2023 05:54:18 GMT',
      'content-type': 'application/json',
      'content-length': '77',
      connection: 'close'
    },
    config: {
      transitional: [Object],
      adapter: [Function (anonymous)],
      transformRequest: [Array],
      transformResponse: [Function: transformResponse],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [Object],
      url: 'https://api.mailjet.com/v3/REST/contactslist/10249638/managecontact',
      params: {},
      data: '{"Name":"New Contact","Properties":{},"Action":"addnoforce","Email":"yosle007+figarog@gmail.com"}',
      method: 'post',
      responseType: 'json',
      auth: [Object]
    },
    request: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: null,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      _header: 'POST /v3/REST/contactslist/10249638/managecontact HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/json\r\n' +
        'User-Agent: mailjet-api-v3-nodejs/6.0.2\r\n' +
        'Content-Length: 97\r\n' +
        'Host: api.mailjet.com\r\n' +
        'Authorization: Basic MTFmMDRiMjgxMjMzMzMwMTc1OGYxMDIwMzc2MGEwMjE6OWQxNWM2MDA5NmJjMjIxYjcxOTM1Y2M2OTU0OGRlNmQ=\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/v3/REST/contactslist/10249638/managecontact',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'api.mailjet.com',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: [Object: null prototype] {
      ErrorInfo: '',
      ErrorMessage: 'Object not found',
      StatusCode: 404
    }
  },
  statusCode: 404,
  statusText: 'Object not found',
  originalMessage: 'Object not found',
  ErrorMessage: 'Object not found'
}
ai-wintermute commented 11 months ago

Hi @yosle looks like it's because you're using an object here: "Properties": {} but it should be a string actually. So, in this case, when you don't have any additional property, you can pass an empty string like this: "Properties": "". Otherwise, you can use a JSON.stringify() function for your object.