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

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

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

以下是完整的输出-

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

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

  1. const express = require("express");
  2. const app = express();
  3. app.use(express.static("public"));
  4. const mailchimp = require("@mailchimp/mailchimp_marketing");
  5. const bodyParser = require("body-parser");
  6. app.use(bodyParser.urlencoded({ extended: true }));
  7. const request = require("request");
  8. const https = require("https");
  9. const axios = require("axios");
  10. const { response } = require("express");
  11. const port = 3000;
  12. app.get("/", (req, res) => {
  13. res.sendFile(__dirname + "/signup.html");
  14. mailchimp.setConfig({
  15. apiKey: 'rupak:ec9e6dcf527*****8655da1b-us13',
  16. server: "us13"
  17. });
  18. });
  19. app.post("/", function(req, res){
  20. const firstName = req.body.firstName;
  21. const lastName = req.body.secondName;
  22. const email = req.body.email;
  23. console.log(firstName, lastName, email);
  24. const data = {
  25. members: [
  26. {
  27. email_address: email,
  28. status: "subscribed",
  29. merge_fields: {
  30. FNAME: firstName,
  31. LNAME: lastName
  32. }
  33. }
  34. ]
  35. }
  36. const jsonData = JSON.stringify(data);
  37. let url = 'https://us13.api.mailchimp.com/3.0/lists/20b****bce/members' ;
  38. const options = {
  39. method: "POST",
  40. auth: "rupak:ec9e6dcf527*****8655da1b-us13"
  41. }
  42. const request = https.request(url, options, (response) => {
  43. response.on("data", (data) => {
  44. console.log(JSON.parse(data));
  45. })
  46. })
  47. request.write(jsonData);
  48. request.end();
  49. });
  50. app.listen(port, () => {
  51. console.log(`Server is running on port ${port}`);
  52. });


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

编辑

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

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


这是更新app.js文件

  1. //jshint esversion: 6
  2. const express = require('express');
  3. const bodyParser = require('body-parser');
  4. const mailchimp = require('@mailchimp/mailchimp_marketing');
  5. const app = express();
  6. app.use(bodyParser.urlencoded({
  7. extended: true
  8. }));
  9. app.use(express.static("public"));
  10. app.get("/", function(req, res) {
  11. res.sendFile(__dirname + "/signup.html");
  12. //setup mailchimp
  13. mailchimp.setConfig({
  14. apiKey: "ec9e6dcf527*****8655da1b-us13",
  15. server: "us13"
  16. })
  17. });
  18. app.post("/", function(req, res) {
  19. var firstName = req.body.firstName;
  20. var lastName = req.body.secondName;
  21. var email = req.body.email;
  22. console.log(`${firstName} ${lastName} ${email}`);
  23. const run = async () => {
  24. //testing if server is working fine
  25. // const response = await mailchimp.ping.get();
  26. // console.log(response);
  27. //add member to list
  28. const response = await mailchimp.lists.addListMember("20b****bce", {
  29. email_address: req.body.email,
  30. status: "subscribed",
  31. merge_fields: {
  32. FNAME: req.body.firstName,
  33. LNAME: req.body.secondName
  34. }
  35. })
  36. console.log(response);
  37. }
  38. run();
  39. });
  40. app.listen(3000, function() {
  41. console.log("Server is running");
  42. });


任何帮助将不胜感激

rnmwe5a2

rnmwe5a21#

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

  1. const data = {
  2. email_address: email,
  3. status: "subscribed",
  4. merge_fields: {
  5. FNAME: firstName,
  6. LNAME: lastName
  7. }
  8. }

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

6ljaweal

6ljaweal2#

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

  1. email_address: req.body.email,
  2. status: "subscribed",
  3. merge_fields: {
  4. FNAME: req.body.firstName,
  5. LNAME: req.body.secondName
  6. }

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

  1. email_address: subscribingUsers.email_address,
  2. status: "subscribed",
  3. merge_fields: {
  4. FNAME: subscribingUsers.firstname,
  5. LNAME: subscribingUsers.lastname,
  6. }


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

展开查看全部
gopyfrb3

gopyfrb33#

这个对我很有效

  1. const data = {
  2. members: [{
  3. email_address: email,
  4. status: "subscribed",
  5. merge_fields: {
  6. FNAME: firstName,
  7. LNAME: lastName,
  8. }
  9. }]
  10. }

字符串

相关问题