javascript 如何解决类型错误:无法读取未定义的属性(读取"fName")

yftpprvb  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(169)

验证码:

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(res, req){
    const fName =  req.body.fName;
    const lName = req.body.lName;
    const email = req.body.email;

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

    const Jsondata = JSONstringify(data);

    const url = "https://us21.api.mailchimp.com/3.0/lists/eca64732d1";

    const options= {
        method: "POST",
        auth:"string1:60ca81db08a41da8672a40261c9d7afd-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 is running on port 3000");
});

//当我运行它时,它显示了一个错误,但我不知道是什么错误,以及如何修复它,加上我需要尽快在这方面的帮助,并且可以在als上的一些人告诉我为什么这个错误来,以便我不再犯这个错误

xe55xuns

xe55xuns1#

问题出在您定义的后处理调用中。参数的顺序错误。如果您读取express docs,则顺序为(req,res),但您使用res作为第一个,这就是无法提取fName的原因。

app.post("/", function(req, res){
    const fName =  req.body.fName;
    const lName = req.body.lName;
    const email = req.body.email;

相关问题