Error while trying to access Mailchimp's API

38 Views Asked by At

I'm following a course on APIs, and I'm trying to make a post request(Batch Subscribe/Unsubscribe) to Mailchimp's API. I did everything that was done in the tutorial and I am not getting the same result as she did.

This is my code:

const express = require ("express")
const bodyParser = require ("body-parser")
const app = express()
const https = require ("https")
const request = require ("request")

app.use(express.static("public"))

app.use(bodyParser.urlencoded({ extended: true }))

app.get("/", function(req, res){
    res.sendFile(`${__dirname}/signup.html`)
})

app.post("/", function(req, res){

    const firstName = req.body.fName
    const lastName = req.body.lName
    const email = req.body.email

    const data = {
        members: [
            {
                email_address: email,
                status: "subscribed",
                merge_fields: {
                    FNAME: firstName,
                    LNAME: lastName
                }
            }
        ]
    }

    const jsonData = JSON.stringify(data);

    const url = 'https://us21.api.mailchimp.com/3.0/lists/f364c63968/'

    const options = {
        method: "POST",
        auth: "Linusman001:c2dcf3fb66c0bc3f918f09bf62c799db-us21"
    }

    const request = https.request(url, options, function(response) {
        response.on("data", function(data){
            console.log(JSON.parse(data));
        })
    })

    request.write(jsonData)
    request.end()
})

app.listen(3000, function(){
    console.log("Server running at port 3000");
})

It returns this to my server and my server crashes:

undefined:1
{"new_members":[{"id":"d02b9864cefa335921f35b1dbb29031a","email_address":"[email protected]","unique_email_id":"a029effe7e","email_type":"html","status":"subscribed","merge_fields":{"FNAME":"g","LNAME":"d","ADDRESS":"","PHONE":"","BIRTHDAY":""},"stats":{"avg_open_rate":0,"avg_click_rate":0},"ip_signup":"","timestamp_signup":"","ip_opt":"105.113.85.118","timestamp_opt":"2024-01-13T22:29:28+00:00","member_rating":2,"last_changed":"2024-01-13T22:29:28+00:00","language":"","vip":false,"email_client":"","location":{"latitude":0,"longitude":0,"gmtoff":0,"dstoff":0,"country_code":"","timezone":""},"tags_count":0,"tags":[],"list_id":"f364c63968","_links":[{"rel":"self","href":"https://us21.api.mailchimp.com/3.0/lists/f364c63968/members/d02b9864cefa335921f35b1dbb29031a","method":"GET","targetSchema":"https://us21.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json"},{"rel":"parent","href":"https://us21.api.mailch

SyntaxError: Unterminated string in JSON at position 929
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\Users\PC\OneDrive\desktop\Newsletter-Signup\app.js:45:30)
    at IncomingMessage.emit (node:events:514:28)
    at Readable.read (node:internal/streams/readable:758:10)
    at flow (node:internal/streams/readable:1248:53)
    at resume_ (node:internal/streams/readable:1227:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

What I expect: No Errors, local server still running (No crashes), browser waits for post response.

0

There are 0 best solutions below