Trying to add a member to a list but I keep getting this error: The resource submitted could not be validated. For field-specific details, see the 'errors' array.
Here is the complete output-
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.'
}
]
}
Here is the app.js file-
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}`);
});
As far as I know, the email_address field is not empty since I have logged the output of email and I can see the email that I entered being displayed.
**** EDIT ****
I tried another method but now I get this long error message that I don't understand at all
Here's a very small part of it
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) {
And here's the updates app.js file
//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");
});
Any help would be appreciated
CodePudding user response:
I think the problem is that you have a members
array, while the api you are using is only accepting one member at a time. It will probably work if you change it to:
const data = {
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
Also, I think you can simply your call by using the @mailchimp/mailchimp_marketing
module the way you find in node.js example in the Mailchimp
documentation. And you are importing Axios, but you are using request
for the call. That package seems to be deprecated.
Hope this helps.
CodePudding user response:
I found the error. Apparently I can't do this directly
email_address: req.body.email,
status: "subscribed",
merge_fields: {
FNAME: req.body.firstName,
LNAME: req.body.secondName
}
I need to create an object of the user and store the values in it. Then I have to access the values from the object while adding a member to the list. Here's how I did it
email_address: subscribingUsers.email_address,
status: "subscribed",
merge_fields: {
FNAME: subscribingUsers.firstname,
LNAME: subscribingUsers.lastname,
}
I would appreciate it if someone told me why this is. Do we need a key-value pair while posting any data to the server? And if so, why doesn't it work by directly getting the values from the request?