Mailchimp batch subscribe error when using node-mailchimp API v2 - json

I'm using the node-mailchimp wrapper for the MailChimp API v2.
I get the following error when calling 'batch-subscribe' or 'batch-unsubscribe' from a function:
-100 'You must specify a id value'
Here's the function:
function doBatchUnsub(theRequest)
{
api.call('lists', 'batch-unsubscribe', theRequest, function (error, data)
{
if (error)
{
errorMessage(error);
}
else
{
console.log(JSON.stringify(data));
}
}
);
}
It actually works fine if I paste the following JSON into the theRequest parameter:
{
"apikey" : "my-key-us8",
"id" : "my-id",
"batch" : [
{
"email" : "example#gmail.com"
},
{
"email" : "example#yahoo.com"
}
],
"delete_member" : true,
"send_goodbye" : false,
"send_notify" : false
}
I only get the error when I pass the JSON into the function from another function. When I do a console.log(theRequest) I can see the exact same text is getting sent to MailChimp. It's confusing.
Any ideas?

Related

Alfresco webscript: AJAX, JSON

I've successfully created a webscript to that returns a JSON response. See below:
get.js file:
// search for folder within Alfresco content repository
var folder = roothome.childByNamePath("PATH");
// validate that folder has been found
if (folder == undefined || !folder.isContainer) {
status.code = 404;
status.message = "Folder " + " not found.";
status.redirect = true;
}
// construct model for response template to render
model.folder = folder;
get.json.ftl:
{"corporates" : [
<#recurse_macro node=folder depth=0/>
]
}
<#macro recurse_macro node depth>
<#list node.children?sort_by(["properties","name"]) as child>
{
"Name" : "${child.properties.name}",
"URL" : "${child.url}",
"serviceURL" : "${child.serviceUrl}",
"shareURL" : "${child.shareUrl}",
"ID" : "${child.id}",
"Type" : "${child.typeShort}"
},
<#if child.isContainer>
{
<#recurse_macro node=child depth=depth+1/>
}
</#if>
</#list>
</#macro>
This returns JSON cleanly (woohoo!), but I would like to grab the JSON from a second webscript using AJAX.
Currently, I am utilizing a typical AJAX call in my second webscript's get.html.ftl file like this:
$(document).ready(function() {
$('.submit-button').click(function(e) {
// Avoid to trigger the default action of the event.
e.preventDefault();
// Actions declared here...
$.ajax({
type: 'GET',
dataType: 'html',
url: 'PLACEHOLDER_URL_PATH',
success: function(data) {
// Shows the result into the result panel.
$('#alfresco-result').html(data);
alert('Done.');
},
error: function(data) {
// Shows the result into the result panel.
$('#alfresco-result').html("ERROR");
}
});
});
})
My question is why the AJAX call doesn't work when I use dataType: 'json'?
I would like to parse through the JSON in my AJAX call and turn it into html (e.g. an html list), but it's not accepting the JSON dataType as an acceptable input.
Any help is appreciated!
You can use POST Webscript Call using ajax like this and pass your jsonObject
dataObj:"yourJsonObject",
to dataObject
Alfresco.util.Ajax.jsonPost(
{
url: Alfresco.constants.PROXY_URI + "sample/mypostwebscript",
dataObj:"yourJsonObject",
successCallback: {
fn: function(res){
alert("success");
alert(res.responseText);
},
scope: this
},
failureCallback:
{
fn: function(response)
{
// Display error message and reload
Alfresco.util.PopupManager.displayPrompt(
{
title: Alfresco.util.message("message.failure", this.name),
text: "search failed"
});
},
scope: this
}
});
},

Cannot read property 'udp' of undefined

I am trying to make a Chrome App to use UDP, but I can not pass simple UDP creation socket process. This is the error I get in the inspector window:
sockets.udp.create: TypeError: Cannot read property 'udp' of undefined
at Object.callback
The error is shown at this line:
chrome.sockets.udp.create({}, function(....
The manifest.json is this:
{
"manifest_version": 2,
"name" : "My App",
"description" : "My App Description",
"version" : "1.0",
"icons" : {
"16" : "icons/wl16.png",
"48" : "icons/wl48.png",
"128" : "icons/wl128.png"
},
"app" : {
"background" : {
"scripts": ["main.js"]
}
},
"sockets" : {
"udp" : {
"send" : ["*"],
"bind" : ["*"]
}
}
The main.js is as follows:
chrome.app.runtime.onLaunched.addListener(function() {
console.log('launched')
sendpack()
})
function sendpack() {
// Create the Socket
chrome.sockets.udp.create({}, function(socketInfo) {
// The socket is created, now we can send some data
var socketId = socketInfo.socketId;
chrome.socket.udp.bind(socketId, '127.0.0.1', 1345, function(result){
console.log('chrome.socket.bind: result = ' + result.toString());
});
var arrayBuffer=new ArrayBuffer(2);
arrayBuffer[0]=65;
arrayBuffer[1]=66;
chrome.sockets.udp.send(socketId, arrayBuffer,'127.0.0.1', 1337,function(sendInfo) {
console.log("sent " + sendInfo.bytesSent);
});
});
}
I copied everything from Chrome examples, but on the examples it works, on my app it doesnt.
If I print on the console the content of the object 'chrome.sockets.udp' it shows a valid object with 'create' method listed in it:
Object {onReceive: Event, onReceiveError: Event}
create: function()
What could be wrong?
I found the error, the bind call had bad object name:
chrome.socket.udp.bind
it should be in plural, "sockets"
chrome.sockets.udp.bind
I also mistakenly read the stack trace, from the bottom, not from the top.

extjs error on filling store

I have a java map. I converted it to json string and I obtain something like this :
{"NEW ZEALAND":"111111111111111","CHAD":"1","MOROCCO":"111","LATVIA":"11"}
Now I want to use it in a store and then a chart like the following code but it's not working. I have no error just no display.
var obj = Ext.Ajax.request({
url: App.rootPath + '/controller/home/dashboard/test.json',
method:'GET',
success: function(response) {
return Ext.JSON.decode(response.responseText);
}
});
var store2 = Ext.create('Ext.data.Store', {
model: 'PopulationPoint',
data: obj
});
Ext.create('Ext.chart.Chart', {
renderTo: 'infos2',
width: 500,
height: 300,
store: store2,
series: [
{
type: 'pie',
field: 'population',
label: {
field: 'state',
display: 'rotate',
font: '12px Arial'
}
}
]
});
The AJAX request is asynchronous. As such, the obj variable used to initialize your data won't contain your data yet.
One option is to create the store2 variable and create the chart directly in the success callback of the AJAX request.
A cleaner option would be to configure the store with a proxy to load the url, and in the callback create the chart.
EDIT
The JSON response does not contain the fields that are declared in your model (sent in the comments). Update the JSON to return a properly formatted model and the chart should work as seen in this fiddle. The JSON should look something like
[
{
"state" : "New Zealand",
"population" : 111111111
},
{
"state" : "Chad",
"population" : 1
}
]

ExtJs, how to send JSON on store.load() using POST method

I need to send JSON object during read operation on store. Headers and method are set correctly.
var proxyDefinition = {
type : 'rest',
api : {
read : '/some/url'
},
actionMethods : {
create : 'POST',
read : 'POST',
update : 'PUT',
destroy : 'DELETE'
},
reader : {
type : 'json'
}
};
var store = Ext.create('Ext.data.Store', {
proxy : proxyDefinition,
model : 'SomeModel'
});
// this needs to send JSON
store.load({
params : {
filter: [] // some filtering rules
}
});
Problem is that POST body is sent as url encoded query string, not JSON object with property "filter".
ExtJs version 4.2.2
It is likely that you are looking for proxy config option paramsAsJson:true

Does Select2 allow for changing name of "text" key to something else?

I have the following Select2 configuration.
$scope.select2Options = {
simple_tags: false,
placeholder : "Search for a language",
multiple : true,
contentType: "application/json; charset=utf-8",
minimumInputLength : 3,
ajax : {
url : "/bignibou/utils/findLanguagesByLanguageStartingWith.json",
dataType : 'json',
data : function(term) {
return {
language : term
};
},
results : function(data, page) {
return {
results :
data.map(function(item) {
return {
id : item.id,
text : item.description
};
}
)};
}
}
};
This allows me to populate the select2 control properly.
However, an issue occurs when I use Ajax in order to post the whole form containing the tags (amongst other): the json array sent to the server contains objects with two properties named id and text whereas the server would require id and description.
see snippet from my json:
"languages":[{"id":46,"text":"Français"},{"id":1,"text":"Anglais"}]
Does select2 allow for changing the name of the text to something else?
Changing my js to the following sorted the issue:
function format(item) { return item.description; };
$scope.select2Options = {
simple_tags: false,
placeholder : "Search for a language",
multiple : true,
contentType: "application/json; charset=utf-8",
minimumInputLength : 3,
data:{ text: "description" },
formatSelection: format,
formatResult: format,
ajax : {
url : "/bignibou/utils/findLanguagesByLanguageStartingWith.json",
dataType : 'json',
data : function(term) {
return {
language : term
};
},
results : function(data, page) {
return {
results :
data.map(function(item) {
return {
id : item.id,
description : item.description
};
}
)};
}
}
};
Notice: one has to use the Select2 top level attribute data.
Here's the bare minium of the configuration neccesary to use a custom id and text properties on ui-select2
$scope.clients: {
data: [{ ClientId: 1, ClientName: "ClientA" }, { ClientId: 2, ClientName: "ClientB" }],
id: 'ClientId',
formatSelection: function (item) { return item.ClientName; },
formatResult: function (item) { return item.ClientName; }
}
Select2 requires that the text that should be displayed for an option is stored in the text property. You can map this property from any existing property using the following JavaScript:
var data = $.map(yourArrayData, function (obj) {
obj.text = obj.text || obj.name; // replace name with the property used for the text
obj.id = obj.id || obj.pk; // replace pk with your identifier
return obj;
});
Documentation