Mr. M
Mr. M

Reputation: 37

How to solve TypeError: request.write is not a function

I am trying to make a newsLetter service using NodeJS & Express by using mailchimp API on hyper shell. I have installed all necessary things including npm,express,request,https module. The code works fine untill when i try to write the user Information in the mailChimp server and showing me the typeError message: request.write() is not a function. Below is my code & the snap of my errorCode.

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

app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));

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 password = req.body.pass;
  const cPassword = req.body.cPass;
  //console.log(firstName);
  //res.send(firstName);



var data = {
    members: [
      {
        email_address: email,
        status: "subscribed",
        merge_fields: {
          FNAME: firstName,
          LNAME: lastName
        }
      }
    ]
  };
  const jsonData = JSON.stringify(data);
  const url = "https://us1.api.mailchimp.com/3.0/lists/97d15bb1ff";
  const options = {
    method: "POST",
    auth: "Mr.M:d2f2f965b9e6b751a305bb6ce2ad7ed4-us1",
  };

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

  request.write(jsonData);
  request.end();
    //res.send("hey")
});

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

Error Message Pictureenter image description here

Upvotes: 0

Views: 6340

Answers (8)

Umar Salau
Umar Salau

Reputation: 1

use

const request = https.request(url, options, function(response)

Upvotes: -1

Sahil Garg
Sahil Garg

Reputation: 1

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

const app=express();
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;

    // console.log(firstName);
    // console.log(lastName);
    // console.log(email);
    var data={
        members:[
            {
                email_address: email,
                status:"subscribed",
                merge_fields:{
                    FNAME:firstName,
                    LNAME:lastName
                }
            }
        ]
    };
    var jsonData=JSON.stringify(data)
    const url="https://us21.api.mailchimp.com/3.0/lists/put your list id";
    const options={
        method:"POST",
        auth:"sahil1:put your api key"
    }


    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 is running on port 3000");
});
In this code

The required modules (express, body-parser, request, and https) are imported.

An instance of the Express application is created by calling express() and assigned to the app variable.

The app instance is configured to serve static files from the "public" directory using express.static() middleware.

The body-parser middleware is added to parse the incoming request bodies.

A route is defined for the root URL ("/") using app.get(). It sends the signup.html file as the response.

Another route is defined for handling the form submission when a POST request is made to the root URL ("/"). The callback function retrieves the form data (first name, last name, and email) from the request body.

The form data is structured in the data variable as per the MailChimp API requirements.

The data is converted to JSON string using JSON.stringify() and stored in the jsonData variable.

The MailChimp API URL and options are defined. In this case, the API key and list ID are hard-coded into the URL and auth options.

An HTTPS request is made to the MailChimp API using https.request(). The URL, options, and a callback function for handling the response are provided as parameters.

In the response callback function, the data received from the MailChimp API is logged after parsing it as JSON.

The jsonData is sent as the request body using request.write().

The request is ended using request.end().

The server starts listening on port 3000 using app.listen(), and a message is logged to indicate that the server is running.

Upvotes: -3

Aditya Aggarwal
Aditya Aggarwal

Reputation: 1

replace the code below:

https.request(url, options, function(response)

with this code:

const request = https.request(url, options, function(response)

Upvotes: 0

Raaz Tuladhar
Raaz Tuladhar

Reputation: 19

You forgot to put in

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

Upvotes: 0

Pedro Silva
Pedro Silva

Reputation: 21

You should have saved the ClientRequest into a variable called request, and after that, you could do the write and end.

Like that :

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

Upvotes: 0

Nosike Adasolum
Nosike Adasolum

Reputation: 1

As already specified by Lingyu Kong, you need to save your request in a constant variable that will allow you to call upon it later:

The node.js website link below illustrates this perfectly:

https://nodejs.org/api/http.html#http_http_request_url_options_callback

Upvotes: 0

Lingyu Kong
Lingyu Kong

Reputation: 1

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

instead use this:

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

});

Upvotes: 0

Antier Solutions
Antier Solutions

Reputation: 1382


res.write(jsonData)

res.end()

use above code instead of request.write(jsonData), request.end().

Upvotes: -1

Related Questions