mailchimp unsubscribe with node js server - javascript

i have a problem with mailchimp unsubscribe process.
Please can you provide me the way for unsubscribe process. How exactly put in unsubscribe list and what files i should request. And how i know the differences that the email should be put in unsubscribe list.
mailchimp.post('/lists/{id}', {
email_address : "email#email.com"
})
.then(function(results) {
console.log(results)
})
.catch(function (err) {
console.log(err)
});
Response error
{ Error: The resource submitted could not be validated. For field-specific details, see the 'errors' array.
at Request._callback (/Users/Gizbreht/Desktop/myofer-mailer-express/node_modules/mailchimp-api-v3/index.js:506:30)
at Request.self.callback (/Users/Gizbreht/Desktop/myofer-mailer-express/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/Gizbreht/Desktop/myofer-mailer-express/node_modules/request/request.js:1126:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/Gizbreht/Desktop/myofer-mailer-express/node_modules/request/request.js:1046:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
title: 'Invalid Resource',
status: 400,
detail: 'The resource submitted could not be validated. For field-specific details, see the \'errors\' array.',
instance: '',
errors:
[ { field: '',
message: 'Required fields were not provided: members' } ] }

mailchimp.post('/lists/{id}', {
"members" : [{"email_address": email, "status": "unsubscribed"}],"update_existing":true
})
.then(function(results) {
console.log(results)
})
.catch(function (err) {
console.log(err)
});

Related

Unable to catch ECONNRESET error and it is causing app to crash

I need help with socket.io-redis-adapter as their document on Client Error Handling seems to be gone.
I do not know how this error with Redis occur, has it got to do with Redis pub/sub? My redis server is in working error through out.
I've searched through forums and github. I've followed the recommended actions as follows. However, I'm still not able to catch the errors below, and it's causing my app to crash. My redis servers are all well and not disconnected at that time as I have other services using same server.
Can anyone provide me with some directions?
"#socket.io/redis-adapter": "^7.0.0",
"#socket.io/redis-emitter": "^4.1.0",
Catching error for both sub and pub client - Done ✅
// I'm using ioredis and I believe ioredis will auto handle reconnection?
pubClient.on("error", (err) => {
debug(`REDIS ADAPTOR DISCONNECTED ON pubClient %O`, err)
})
subClient.on("error", (err) => {
debug(`REDIS ADAPTOR DISCONNECTED ON subClient %O`, err)
})
Catch error on adapter - Done ✅
// But i think this is not working to catch errors ❌❌❌
io.of('/').adapter.on('error', function(error){
debug('error: ', error);
});
Ping to keep connection alive - Done ✅
setInterval(() => {
// emitter.of('/user').emit("time", new Date);
io.of('/user').emit("time", new Date())
}, 300000);
I've also tried using both node-redis and ioredis and both end up with same error below Done ✅
Manage to catch this with redis on error event ✅
socket REDIS ADAPTOR DISCONNECTED ON pubClient Error: read ECONNRESET
socket at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
socket errno: -54,
socket code: 'ECONNRESET',
socket syscall: 'read'
socket } +5h
socket error: Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
errno: -54,
code: 'ECONNRESET',
syscall: 'read'
} +0ms
// Unable to catch this event ❌❌❌
// I think this causes my app to crash
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted 'error' event on RedisAdapter instance at:
at Redis.onError (/Users/Myname/Work/expressjs/api.singa.sg/node_modules/#socket.io/redis-adapter/dist/index.js:61:22)
at Redis.emit (events.js:327:22)
at Redis.EventEmitter.emit (domain.js:486:12)
at Redis.silentEmit (/Users/Myname/Work/expressjs/api.singa.sg/node_modules/ioredis/built/redis/index.js:544:26)
at Socket.<anonymous> (/Users/Myname/Work/expressjs/api.singa.sg/node_modules/ioredis/built/redis/event_handler.js:190:14)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:327:22)
at Socket.EventEmitter.emit (domain.js:486:12)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -54,
code: 'ECONNRESET',
syscall: 'read'
}
This is snippet of how my server is created;
....server declaration codes
const { createAdapter } = require("#socket.io/redis-adapter");
const Redis = require("ioredis");
const pubClient = new Redis({
host: process.env.SOCKET_IO_REDIS_HOST,
port: process.env.SOCKET_IO_REDIS_PORT,
username: process.env.SOCKET_IO_REDIS_USER,
password: process.env.SOCKET_IO_REDIS_PASSWORD,
db: process.env.SOCKET_IO_REDIS_DB,
})
const subClient = pubClient.duplicate();
const adapter = createAdapter(pubClient, subClient)
io.adapter(adapter)
pubClient.on("ready", () => {
debug(`REDIS ADAPTOR CONNECTED`)
})
pubClient.on("error", (err) => {
debug(`REDIS ADAPTOR DISCONNECTED ON pubClient %O`, err)
})
subClient.on("error", (err) => {
debug(`REDIS ADAPTOR DISCONNECTED ON subClient %O`, err)
})
pubClient.on("end", (err) => {
debug(`REDIS ADAPTOR ENDED %O`, err)
})
setInterval(() => {
// emitter.of('/user').emit("time", new Date);
io.of('/user').emit("time", new Date())
}, 300000);
io.of('/').adapter.on('error', function(error){
debug('error: ', error);
});

Request.put() JSON error (Node.JS, Microsoft Graph API - Assign Manager)

I'm working on a Node.js app that makes request to the Microsoft Graph API to create Azure AD users. Authentication and creating users is working fine. The problem comes when I make a put request to (https://graph.microsoft.com/v1.0/users/{id}/manager/$ref) . I get this error.
StatusCodeError: 400 - "{\r\n \"error\": {\r\n \"code\": \"Request_BadRequest\",\r\n \"message\": \"An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected.\",\r\n \"innerError\": {\r\n \"request-id\": \"6245c337-a157-49a8-9b59-bdd2bf5eea01\",\r\n \"date\": \"2019-07-17T19:31:20\"\r\n }\r\n }\r\n}"
at new StatusCodeError (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\errors.js:32:15)
at Request.plumbing.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:104:33)
at Request.RP$callback [as _callback] (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:46:31)
at Request.self.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at Request.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1161:10)
at Request.emit (events.js:198:13)
at IncomingMessage.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:286:20)
at IncomingMessage.emit (events.js:203:15)
Here's the code. I'm using request-promise-native.
user.assignManager = function(id, manager){
auth.getToken().then(token => {
request.put('https://graph.microsoft.com/v1.0/users/' + id + '/manager/$ref', {
auth: {
bearer: token
},
body : manager,
headers: {
'Content-type': 'application/json'
}
}).then(value =>{
console.log('Assigned Manager');
}).catch(err =>{
console.log(err);
})
}).catch(err => {
console.log(err);
});
}
'manager' that I'm putting in the body
{
"#odata.context":"https://graph.microsoft.com/v1.0/$metadata#directoryObjects/$entity",
"#odata.type":"#microsoft.graph.user",
"id":"2db4c4e2-a349-4eb0-97d0-862143f5b01d",
"businessPhones":[],
"displayName":"John Smith",
"givenName":"John",
"jobTitle":"Intern",
"mail":null,
"mobilePhone":null,
"officeLocation":"BR",
"preferredLanguage":null,
"surname":"Smith",
"userPrincipalName":"JohnSmith#test1234512gmail.onmicrosoft.com"
}
I also tried like this instead of as a user object.
{
"id": "2db4c4e2-a349-4eb0-97d0-862143f5b01d"
}
I'm assuming its some kind of parsing issue, but I'm at my wits end. Any ideas?
Got it working.
Add json: true, to the request and the body needs to be
{ "#odata.id": "https://graph.microsoft.com/v1.0/users/{id}" }

I'm getting a Error using node package mysql v2.16.0 while connecting to database Error: Packets out of order. Got: 80 Expected: 0

versions:
node > 11.4.0
mysql(node package) > 2.16.0
phpMyAdmin > 4.8.3
MySQL > 5.7
I'm using this code:
const mysql = require('mysql');
let db = mysql.createConnection({
host:'hidden',
port:'hidden',
user:'hidden',
password:'hidden',
database: 'hidden',
dubug: true
});
sqlfn.createTable('Internal', db, 'test_table', (result, err) => {
if(err) {console.log(err.stack); return};
console.log('Create OK: ', result);
});
From the function file sqlfn:
exports.createTable = (req, db, name, callback) => {
let query_string = String(`CREATE TABLE ${name} (uid VARCHAR(255), name VARCHAR(255), type VARCHAR(255), data VARCHAR(255), modified VARCHAR(255))`);
db.query(query_string, (err, result) => {
if(err) fn.logit(req, 'SQL', 'error', `in <createTable> ${err}`);
callback(result, err);
});
};
This is the error that I am getting:
>
Error: Packets out of order. Got: 80 Expected: 0
at Parser.write (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Parser.js:42:19)
at Protocol.write (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:91:28)
at Socket. (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:502:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)
--------------------
at Protocol._enqueue (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Protocol.handshake (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:51:23) at Connection.connect (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:118:18)
at Connection._implyConnect (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:453:10) at Connection.query (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:198:8)
at Object.exports.createTable (C:\Users\hidden\Documents\Git\hidden\components\functions\sql.js:36:8)
at Object. (C:\Users\hidden\Documents\Git\hidden\components\controllers\controll_admin.js:22:7)
at Module._compile (internal/modules/cjs/loader.js:723:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
If anyone know how i can fix or work-around this I'd love some feedback and help regarding this.
thank you.
:)
I was getting the same error and it was because I was not setting the correct port for my database which was 3305 instead of the default 3306

Node.js MSSQL 500: Internal Error. Details: ConnectionError: Failed to connect to DESKTOP-LK9JDJJ:1433 - connect ECONNREFUSED 192.168.99.1:1433

settings.js
exports.dbConfig = {
user: "gpolat",
password: "alphalpha",
server: "DESKTOP-LK9JDJJ",
database: "mydemo",
port: 1433
};
exports.webPort = 9000;
db.js
var sqlDb = require("mssql");
var settings = require("../settings");
exports.executeSql = function (sql, callback) {
var conn = new sqlDb.Connection(settings.dbConfig);
conn.connect()
.then(function () {
var req = new sqlDb.Request(conn);
req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function (err) {
console.log(err);
callback(null, err);
});
})
.catch(function (err) {
console.log(err);
callback(null, err);
});
};
Console Error:
Debugger listening on 127.0.0.1:5858
Started Listening at: 9000
{ ConnectionError: Failed to connect to DESKTOP-LK9JDJJ:1433 - connect
ECONNREFUSED 192.168.99.1:1433
at Connection.<anonymous> (C:\Users\Gokhan\documents\visual studio
2015\Projects\SampleREST\SampleREST\node_modules\mssql\lib\tedious.js:378:25)
at Object.onceWrapper (events.js:290:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (C:\Users\Gokhan\documents\visual studio
Projects\SampleREST\SampleREST\node_modules\tedious\lib\connection.js:531:1
4)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1281:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'ConnectionError',
message: 'Failed to connect to DESKTOP-LK9JDJJ:1433 - connect ECONNREFUSED
192.168.99.1:1433',
code: 'ESOCKET' }
I get this error while creating the rest API via sql server.
"500: Internal Error. Details: ConnectionError: Failed to connect to DESKTOP-LK9JDJJ:1433 - connect ECONNREFUSED 192.168.99.1:1433"

Node.js Request error - ECONNRESET

Every now and then, as I'm requesting data from a github-issue, I get this error when firing my request-promise-function:
{ RequestError: Error: read ECONNRESET
at new RequestError (/vagrant/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/vagrant/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/vagrant/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/vagrant/node_modules/request/request.js:187:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:191:7)
at Request.onRequestError (/vagrant/node_modules/request/request.js:859:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at TLSSocket.socketErrorListener (_http_client.js:358:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at emitErrorNT (net.js:1283:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
From previous event:
at Request.plumbing.init (/vagrant/node_modules/request-promise-core/lib/plumbing.js:36:28)
at Request.RP$initInterceptor [as init] (/vagrant/node_modules/request-promise-core/configure/request2.js:41:27)
at new Request (/vagrant/node_modules/request/request.js:129:8)
at request (/vagrant/node_modules/request/index.js:54:10)
at module.exports (/vagrant/resources/GetIssuesData.js:25:9)
at Socket.<anonymous> (/vagrant/app.js:42:13)
at emitNone (events.js:86:13)
at Socket.emit (events.js:188:7)
at /vagrant/node_modules/socket.io/lib/socket.js:503:12
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
name: 'RequestError',
message: 'Error: read ECONNRESET',
cause:
{ Error: read ECONNRESET
at exports._errnoException (util.js:1029:11)
at TLSWrap.onread (net.js:575:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' },
error:
{ Error: read ECONNRESET
at exports._errnoException (util.js:1029:11)
at TLSWrap.onread (net.js:575:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' },
options:
{ uri: 'https://api.github.com/repos/1dv023/jp222vq-examination-3/issues?access_token=TOKEN',
headers:
{ 'User-Agent': 'me',
'Content-Type': 'application/x-www-form-urlencoded' },
simple: false,
reconnect: true,
callback: [Function: RP$callback],
transform: undefined,
resolveWithFullResponse: false,
transform2xxOnly: false },
response: undefined }
The request is fired every time a user requests a new page on the application, and is also fired when changes are made in the github-issue. The request feeds a socket-eventemitter with data. This is the request-function:
(function() {
let rp = require("request-promise");
module.exports = function(res, callback) {
let options = {
uri: "https://api.github.com/repos/" + res.issue,
headers: {
"User-Agent": "Me",
"Content-Type": "application/x-www-form-urlencoded"
},
simple: false,
reconnect: true
};
rp(options)
.then(function(body, err) {
try {
body = JSON.parse(body);
let context = {
issues: body.map(function(issue) {
return {
title: issue.title,
comments: issue.comments,
uploaded: issue.created_at,
updated: issue.updated_at,
link: "/" + issue.number
};
}),
};
callback(context, null);
} catch (e) {
throw new Error(e)
}
})
.catch(function(err) {
callback(null, err);
});
};
}());
I've been scratching my head for ages now, trying to understand why I get this error. Tried to read up on ECONNRESET, but didn't quite understand it. Can anybody please help?

Categories

Resources