Add Existing Tag/Segment To Contacts - node.js

Lately I have found mailchimp-api-v3 to be quite useful for managing our .1k list. Currently, I use the following to (1) create new tags, and (2) add the tag to contacts:
const MC = require('mailchimp-api-v3');
const mailchimp = new MC('<apiKey>');
mailchimp.batch([{
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag1>',
static_segment: [<contact_list1>]
}
}, {
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag2>',
static_segment: [<contact_list2>]
}
}])
.then(results => {
console.log( results );
})
.catch(errs => {
console.log( errs );
});
Sometimes there's need to add an existing tag to contacts. Whenever I try to use the above code, as expected, I get tag already exists error and contacts are not tagged with this existing tag.
How do I get a list of all existing tags? And how would one add an existing tag to contacts?

I finally figured out how to do that. The just updated documentation is much easier to navigate that before. It's now evident that there are two ways of adding a tag/segment to (a) member(s): you can add a single tag to a member or you can add a single tag to many members:
Bulk add with members_to_add: array
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}',
body: {members_to_add: [<email-addresses>]}
}
This is indeed what I needed and I have now been using it for a while. Just today I found out the other method:
Single add with email_address: string
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}/members',
email_address: {email_address}
}

Related

Is there any way to get content type's data (dropdown data)?

I'm working on contentful to create blog posts.
I have created a field named with category with dropdown data, like the below image.
I have created more blogs for each categories (Ex: game has 5 blogs, Tour has 10 blogs and etc).
I want to show the list of all categories with content counts,
Is there any possible to get all of the categories with content count? ( I can get it by getting all blogs using this query
const res = ContentfulService.getEntries({ content_type: 'blog'})
then I grouped with category, but to get the category only, I don't want to get all of the blogs.)
Please let me know if there is a solution.
Thanks
The only way to do this through the API would be to make a request for each category and look at the total property of the response and that would be less efficient than what you're already suggesting.
https://cdn.contentful.com/spaces/{space_id}/environments/{environment_id}/entries?access_token={access_token}&content_type={content_type}&fields.category[in]={categoryValue}
I have achieved that using graphQL,
const query =
{
categoryCollection
{
items{
linkedFrom {
blogCollection {
total
}
}
name:category
sys {
id
}
}
}
}
let categories = await fetch(https://graphql.contentful.com/content/v1/spaces/${spaceId}, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Authenticate the request
Authorization: Bearer ${accessToken},
},
// send the GraphQL query
body: JSON.stringify({ query }),
})

AWS DynamoDB NodeJS Transactions - Trying to create 'move' with Put & Delete but ValidationError

I'm trying to do a simple transactional 'move' of data from one table to another in DynamoDB (to add indexes). Here's the call I'm making, the promise is returned because I'm using .then() to interpret it, but as you can see from the result I'm getting back there's something wrong with my JSON. Unfortunately, the error message is not very helpful. Very grateful of any help.
var runner = async (column1Value, column2Value) => {
return await ddb.transactWriteItems({
TransactItems: [
{
Delete: {
TableName: 'TABLE_V2',
Key: {
AuthorName: {
S: column1Value
}
}
}
}
,
{
Put: {
Item: {
'AuthorName': {
S: column1Value
},
'AuthorTitle': {
S: column2Value
}
},
TableName: 'TABLE_V3'
}
}
]
}).promise();
}
When I run my code I get the following:
at Request.extractError...
message: 'Transaction cancelled, please refer cancellation reasons for specific reasons [ValidationError, None]',
code: 'TransactionCanceledException',
time: 2019-03-31T00:03:05.401Z,
requestId: 'URIARNJDVFHDVLQQNR1JMIJP5NVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 400,
retryable: false,
retryDelay: 30.057548210067033
I've read a few sites out there but all refer to a time froms before DynamoDB natively supported transactions with the .transactWriteItems().
e.g.: How to support transactions in dynamoDB with javascript aws-sdk?
Ok, so, there were two issues (let me know if they are not relevant to this site).
The 'Put' error was being thrown by the fact that there was a space character after the column name for the first column: 'AuthorName '
The 'Delete' error was being thrown by the fact that the TABLE_V2 is using not just a primary key but a search key also, so its JSON needs to be:
Delete: {
TableName: 'TABLE_V2',
Key: {
AuthorName: {
S: column1Value
},
AuthorTitle: {
S: column2Value
}
}
}

nodeJS loopback include filter

i have a problem with Node JS loop-back include filter when i make a query .Every time its giving all records in response.but i want only those records that holds the sportid which i am passing in query filter.
var request = {
method: 'get',
command: 'UserPersonalinfos',
query: {
filter: {
"include": {
"relation": "UserRegistration",
"where": {
"sportid": data[0].id
}
},
limit:5
}
},
headers: {
access_token:userAccessToken.id,
}
};
if sportid exists then it should return object otherwise it should not, but in this case its returns all UserPersonalinfos records and adding UserRegistration object to UserPersonalinfos object
As per https://stackoverflow.com/a/32933383/344022 there's nothing officially available yet. There is a fork of the loopback-connector by #DiogoDoreto that does attempt to provide it. I haven't tried it, but if you were to use it you would do the following in a filter:
"where": {
"UserRegistration": {
"where": {
"sportid": data[0].id
}
}
}
The answer linked above also suggests some other ways of achieving what you want.

select2 remove tag_list item

Trying to get Select2 to remove an item and figure out why it keeps adding a new tag on update.
Using Select2.JS V3.5
Followed ActiveAdmin / Select2 example
When I try to delete an item, it does not remove it. It also ends up combining the tag list to a new STRING creating a new tag. i.e. If I have ["play", "doe"] and I update another field, it creates a new tag called "play doe" resulting in ["play", "doe", "play doe"]. This continues every time I update.
Here's my JS code
// Generated by CoffeeScript 1.9.3
$(document).ready(function() {
$('.tagselect').each(function() {
var placeholder, saved, url;
placeholder = $(this).data('placeholder');
url = $(this).data('url');
saved = $(this).data('saved');
$(this).select2({
tags: true,
placeholder: placeholder,
minimumInputLength: 3,
allowClear: true,
multiple: true,
debug: true,
initSelection: function(element, callback) {
saved && callback(saved);
},
ajax: {
url: url,
dataType: 'json',
data: function(term) {
return {
q: term
};
},
results: function(data) {
return {
results: data
};
}
},
createSearchChoice: function(term, data) {
if ($(data).filter((function() {
return this.text.localeCompare(term) === 0;
})).length === 0) {
return {
id: term,
text: term
};
}
}
});
});
});
Saw https://github.com/mbleigh/acts-as-taggable-on/issues/676
adding the block in my model, fixes the formtastic bug where all tags where coming in as a flat string vs a comma separated string. Monkey Patch for now.
class MyModel < ActiveRecord::Base
...
def tag_list
super.to_s
end
end

AngularJS and Express - Routing multiple urls

I'm currently trying to write a service with the MEAN stack to handle actions on a game. I've followed boiler plates up to the point where I now need to add custom actions.
I have a game model which I can do the usual, save, update, delete on. But I want to add actions like join/leave game. I have tried this currently, for the game service:
window.angular.module('ngff.services.games', [])
.factory('Games', ['$resource',
function($resource){
return $resource(
'games/:gameId/:actionId',
{
gameId: '#_id',
actionId: '#actionId'
},
{
update: {
method: 'PUT',
params: {
actionId: null
}
},
join: {
method: 'POST',
params: {
actionId: 'join'
}
},
leave: {
method: 'POST',
params: {
actionId: 'leave'
}
}
}
);
}]);
Update works correctly, but my other requests always seems to default post to games/:gamesId without adding the actionId on the end. How can I change the location that my form posts to?
I already have the backend worked out, and manually posting to my apis works fine.
I had all this working without angular, but I've never used it before and I'm a little stuck.
Thanks
Ryan

Resources