I'm bulding my api server for my React/Redux app, where i need some flat response from server to manage and reduce my state.
For example, when i have this response:
[{
id: 1,
title: 'Some Article',
author: {
id: 1,
name: 'Dan'
}
}, {
id: 2,
title: 'Other Article',
author: {
id: 1,
name: 'Dan'
}
}]
How can i make it looks like:
{
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {
id: 2,
title: 'Other Article',
author: 1
}
},
users: {
1: {
id: 1,
name: 'Dan'
}
}
}
}
Best feature is what each key is articles/users id, so i can easily get it from the response and merge it in my front-end app.
You can achieve this with AMS, but you will have to write a custom adapter, as none of the stock ones provide this exact format.
JSON API (and the corresponding AMS JsonApi adapter) solve this issue, but the format is slightly different from the one you want.
Related
I have Many-to-many relations with custom properties like here
I try to get a post with all category like
PostRepository.findOne({
relations: ['postToCategories.category'],
where: { id: 1},
})
the results like this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, category: {id: 1, name: "cate1"}}]
}
But i don't want id of intermediate table. I need something like this
{
id: 1,
name: 'post1',
postToCategories: [{category: {id: 1, name: "cate1"}}]
}
or this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, name: "cate1"}]
}
I don't need id of postToCategories. How to do that thank.
I'm creating Carousel Ad via marketing API. During the Ad creation Facebook through "Creative and Objective Mis-match" error. However, Campaign and Adset are successfully creating.
I have changed the objective as per Facebook documentation. I tried multiple combinations but got errors. e.g: "Billing event invalid for optimisation goal, Creative and Objective Mismatch, Invalid Creative for Objective"
Doc_link
res = ad_account.ads.create({
status: 'ACTIVE',
name: ' SDK',
adset_id: adset.id,
creative: {
object_story_spec: {
page_id: fb_page_id,
link_data: {
child_attachments: [
{
description: "$8.99",
image_hash: "1ea34798e1ba00ffaab318dc",
link: "www.facebook.com",
name: "Product 1",
video_id: "42920500794",
call_to_action: {
type: "LEARN_MORE",
value: {
link: "www.facebook.com"
}
}
},
{
description: "$9.99",
image_hash: "8299deb1e053f7601c4f99f44",
link: "www.facebook.com",
name: "Product 2",
video_id: "378442876",
call_to_action: {
type: "LEARN_MORE",
value: {
link: "www.facebook.com"
}
}
},
],
:link => "www.facebook.com",
},
}
}
})
I need to create a complete carousel ad by using marketing API.
The loading array data example from the official documenation is just not working for me.
https://jsfiddle.net/Jngai1297/v64rwj7b/7/
html
<select class="js-example-data-array-selected">
<option value="2" selected="selected">duplicate</option>
</select
javascript
var hidata = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
$(".js-example-data-array").select2({
data: hidata
})
$(".js-example-data-array-selected").select2({
data: hidata
})
The examples are straight out from the doc and the external resources are correct as well. Been hacking on it half the day and searched around and found nothing.
$(document).ready(function() {
var airPorts = [
{id:0,text:'airport one'},
{id:1,text:'airport two'},
{id:2,text:'airport three'},
{id:3,text:'airport four'},
];
$('.select2').select2({
data: airPorts,
});
});
I'm building a REST api with grails 3 and mongo. I have encountered a problem when i need to marshal an object graph with a bigger depth.
I have the following domains:
class Category extends Resource {
/* other fields */
Category parent
}
class Product extends Resource {
/* other fields */
List<Category> categories
static hasMany = [categories: Category]
}
I have in the database the following structure(simplified for the sake of understanding):
categories:
{name: 'cat1'}
{name: 'cat2', parent: 'cat3'}
{name: 'cat3', parent: 'cat4'}
{name: 'cat4', parent: 'cat5'}
{name: 'cat5'}
product:
{categories: ['cat1', 'cat2']}
I am extending from RestfullController when creating my controllers. I want to be able to get a product and have the categories with parents in the returned json.
I get the following results:
/product/${id}
{
id: '...',
categories: [{
id: '...',
name: 'cat1'
}, {
id: '...',
name: 'cat2',
parent: { id: '...' }
}]
}
/category/cat2id
{
id: '...',
name: 'cat2',
parent: { id: '...' }
}
/category
[{
id: '...',
name: 'cat1'
},{
id: '...',
name: 'cat5'
},{
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
},{
id: '...',
name: 'cat3',
parent: {
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
}
},{
id: '...',
name: 'cat2',
parent: {
id: '...',
name: 'cat3',
parent: {
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
}
}
}]
Why would Category.list() load the whole category object graph, and Category.get(), Product.get() and Product.list() would not load it? Is there a way to control this behaviour ?
The way Grails works is that it will only render associations that have already been loaded from the database, hence why you get some associations rendered and others not.
There is no built in way to control this behaviour other than writing your own marshaller. See http://grails.github.io/grails-doc/latest/guide/webServices.html#renderers
So I'm using the yelp API, and after I make a GET request I get back a response of businesses. In order to work with that response I'm using .map
Example:
mappedResults = yelpSearch.businesses.map {|l| {id: l.id, name: l.name, categories:l.categories, rating: l.rating, review_count: l.review_count, url: l.url, phone: l.phone}}
My problem is that sometimes l.phone is not returned for some records in the response, and I get the error:
undefined method `phone' for #<BurstStruct::Burst:0x007fba47c7a228>
My question is how do I refactor this code so that if a record doesn't have phone it will either leave it null (or worst cast empty string)
Any help is appreciated
JSON structure is as such for each business in the response
{
region: {
span: {
latitude_delta: 0,
longitude_delta: 0
},
center: {
latitude: 38.054117,
longitude: -84.439002
}
},
total: 23,
businesses: [
{
is_claimed: false,
rating: 5,
mobile_url: "http://m.yelp.com/biz/vineyard-community-church-lexington",
rating_img_url: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
review_count: 2,
name: "Vineyard Community Church",
snippet_image_url: "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
rating_img_url_small: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
url: "http://www.yelp.com/biz/vineyard-community-church-lexington",
phone: "8592582300",
snippet_text: "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
image_url: "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
categories: [
[
"Churches",
"churches"
]
],
display_phone: "+1-859-258-2300",
rating_img_url_large: "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
id: "vineyard-community-church-lexington",
is_closed: false,
location: {
city: "Lexington",
display_address: [
"1881 Eastland Pwky",
"Lexington, KY 40505"
],
geo_accuracy: 8,
postal_code: "40505",
country_code: "US",
address: [
"1881 Eastland Pwky"
],
coordinate: {
latitude: 38.054117,
longitude: -84.439002
},
state_code: "KY"
}
}
]
}
If you're using Rails 4.0 or newer, the #presence method is really helpful for this. You would use it like this:
mappedResults = yelpSearch.businesses.map {|l| {id: l.id.presence, #... etc
or like this
mappedResults = yelpSearch.businesses.map {|l| {id: l.id.presence || "default id", # ...
Update
Reading your code again, #presence might not work in this case, since the method isn't defined. Here's a longer (uglier) snippet that should work:
mappedResults = yelpSearch.businesses.map do |l|
id: l.respond_to(:id) ? l.id : "default id",
# ... other properties
end
Update from OP
This worked - thank you! Note I had to tweak syntax a bit to respond_to?('method_name')
mappedResults = yelpSearch.businesses.map {|l|
{
name: l.respond_to?("name") ? l.name : "nameless",
rating: l.respond_to?("rating") ? l.rating : "unrated",
# ... other properties
}}