NodeJS Mailchimp:无法验证提交的资源,有关特定于字段的详细信息,请参阅“errors”数组

uqcuzwp8  于 2023-11-17  发布在  Node.js
关注(0)|答案(3)|浏览(116)

尝试将成员添加到列表中,但一直收到此错误:无法验证提交的资源。有关特定字段的详细信息,请参阅'errors'数组。

以下是完整的输出-

Server is running on port 3000
John Doe [email protected]
{
  type: 'https://mailchimp.com/developer/marketing/docs/errors/',
  title: 'Invalid Resource',
  status: 400,
  detail: "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
  instance: 'd2186fc0-10d0-22af-ac4b-b04d7c6a61b7',
  errors: [
    {
      field: 'email_address',
      message: 'This value should not be blank.'
    }
  ]
}

字符串
下面是app.js文件:

const express = require("express");
const app = express();

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

const mailchimp = require("@mailchimp/mailchimp_marketing");

const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));

const request = require("request");
const https = require("https");

const axios = require("axios");
const { response } = require("express");

const port = 3000;

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/signup.html");

  mailchimp.setConfig({
    apiKey: 'rupak:ec9e6dcf527*****8655da1b-us13',
    server: "us13"
  });
});

app.post("/", function(req, res){
    const firstName = req.body.firstName;
    const lastName = req.body.secondName;
    const email = req.body.email;
    console.log(firstName, lastName, email);
   
    const data = {
      members: [
        {
            email_address: email,
            status: "subscribed",
            merge_fields: {
                FNAME: firstName,
                LNAME: lastName
            }
        }
      ]
    }

    const jsonData = JSON.stringify(data);
    
    let url = 'https://us13.api.mailchimp.com/3.0/lists/20b****bce/members' ;

    const options = {
        method: "POST",
        auth: "rupak:ec9e6dcf527*****8655da1b-us13"
    }

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

    request.write(jsonData);
    request.end();

  });

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});


据我所知,email_address字段不是空的,因为我已经记录了电子邮件的输出,我可以看到我输入的电子邮件正在显示。

编辑

我尝试了另一个method,但现在我得到了这个很长的错误消息,我根本不明白
这里有一小部分

Error: Bad Request
    at Request.callback (C:\Users\Rupak\Desktop\Projects\Web Development\Newsletter-Signup\node_modules\superagent\lib\node\index.js:699:13)
    at C:\Users\Rupak\Desktop\Projects\Web Development\Newsletter-Signup\node_modules\superagent\lib\node\index.js:903:18
    at Stream.<anonymous> (C:\Users\Rupak\Desktop\Projects\Web Development\Newsletter-Signup\node_modules\superagent\lib\node\parsers\json.js:19:7)
    at Stream.emit (node:events:390:28)
    at Unzip.<anonymous> (C:\Users\Rupak\Desktop\Projects\Web Development\Newsletter-Signup\node_modules\superagent\lib\node\unzip.js:55:12)
    at Unzip.emit (node:events:390:28)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {


这是更新app.js文件

//jshint esversion: 6

const express = require('express');
const bodyParser = require('body-parser');
const mailchimp = require('@mailchimp/mailchimp_marketing');

const app = express();

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

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

app.get("/", function(req, res) {
  res.sendFile(__dirname + "/signup.html");

  //setup mailchimp
  mailchimp.setConfig({
    apiKey: "ec9e6dcf527*****8655da1b-us13",
    server: "us13"
  })
});

app.post("/", function(req, res) {
  var firstName = req.body.firstName;
  var lastName = req.body.secondName;
  var email = req.body.email;

  console.log(`${firstName} ${lastName} ${email}`);

  const run = async () => {
    //testing if server is working fine

    // const response = await mailchimp.ping.get();
    // console.log(response);

    //add member to list
    const response = await mailchimp.lists.addListMember("20b****bce", {
      email_address: req.body.email,
      status: "subscribed",
      merge_fields: {
        FNAME: req.body.firstName,
        LNAME: req.body.secondName
      }
    })

    console.log(response);
  }

  run();
  
});

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


任何帮助将不胜感激

rnmwe5a2

rnmwe5a21#

我认为问题是你有一个members数组,而the api you are using一次只接受一个成员。如果你把它改为:

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

字符串
此外,我认为您可以通过使用@mailchimp/mailchimp_marketing模块来简化您的调用,就像您在the Mailchimp documentation中的node.js示例中找到的那样。您正在导入Axios,但您正在使用request进行调用。That package seems to be deprecated
希望这对你有帮助。

6ljaweal

6ljaweal2#

我发现了错误。显然我不能直接这样做

email_address: req.body.email,
status: "subscribed",
merge_fields: {
  FNAME: req.body.firstName,
  LNAME: req.body.secondName
}

字符串
我需要创建一个用户的对象并将值存储在其中。然后我必须在向列表添加成员时访问对象中的值。

email_address: subscribingUsers.email_address,
  status: "subscribed",
  merge_fields: {
    FNAME: subscribingUsers.firstname,
    LNAME: subscribingUsers.lastname,
}


如果有人能告诉我这是为什么,我会很感激。我们在向服务器发送数据时需要键值对吗?如果需要,为什么不直接从请求中获取值?

gopyfrb3

gopyfrb33#

这个对我很有效

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

字符串

相关问题