Eventbrite API Request Parameter - ajax

I want to get the list of popular events in San Francisco through
ajax request to Eventbrite API. Here's the code.
var city = 'San Francisco';
var query = "token=" + token + "&venue.city=" + city + "&popular=" + true + "&location.within=" + "10mi"
var $events = $("#events");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://www.eventbriteapi.com/v3/events/?" + query,
"method": "GET",
"headers":{}
}
I'm getting results from other cities too. How can I set parameters to get results from San Francisco only? Also, I'm getting multiple copies of a single event. Is there any way to prevent this?

Your Query is Incorrect for API v3: When testing your query I received errors from the Eventbrite API. These errors are due to using the incorrect query parameters. For example 'popular' does not exist as a parameter. Make sure to test your queries via their playground https://www.eventbriteapi.com/v3/events/search?token='YOUR-OAUTH-TOKEN'.
SF Only Events: There is no way to set parameters in the Eventbrite query for events only in SF unless you include a small search radius around the latitude and longitude. That method could possibly exclude some popular SF events. The best way is to query all SF area events and then manipulate the data you get back by filtering out events whose Venue city is not San Francisco.
Popular Events: To truly get popular events you'd need to know how many tickets have been sold for each event (private data.) Instead, we need to use the sort_by parameter in our initial query to get the 'best' events. Unfortunately, 'best' is very vague in the API but all we have to work with.
Duplicate Events: Given the below query you will not get duplicate events.
Try the below jQuery AJAX method:
const sfSettings = {
url: 'https://www.eventbriteapi.com/v3/events/search/',
data: {token: 'YOUR-OAUTH-TOKEN', sort_by: 'best', 'location.latitude': 37.7749, 'location.longitude': -122.4194, expand: 'venue'},
crossDomain: true,
method: 'GET'
}
$.ajax(sfSettings).done(function(eventObject){
// All SF Area Events (Paginated by 50. Will only return first page.)
const events = eventObject.events;
// Create a new array of events whose venue is specifically in SF
const sfEvents = events.filter(function(event){
return event.venue.address.city === 'San Francisco';
});
});

Related

How to get query sys_id of current.sys_id Service Portal (ServiceNow)

I have a question regarding a small issue that I'm having. I've created a widget that will live on the Service Portal to allow an admin to Accept or Reject requests.
The data for the widget is pulling from the Approvals (approval_approver) table. Under my GlideRecord, I have a query that checks for the state as requested. (Ex. addQuery('state', 'requested'))
To narrow down the search, I tried entering addQuery('sys_id', current.sys_id). When I use this query, my script breaks and I get an error on the Service Portal end.
Here's a sample of the GlideRecord script I've written to Accept.
[//Accept Request
if(input && input.action=="acceptApproval") {
var inRec1 = new GlideRecord('sysapproval_approver');
inRec1.addQuery('state', 'requested');
//inRec1.get('sys_id', current.sys_id);
inRec1.query();
if(inRec1.next()) {
inRec1.setValue('state', 'Approved');
inRec1.setValue('approver', gs.getUserID());
gs.addInfoMessage("Accept Approval Processed");
inRec1.update();
}
}][1]
I've research the web, tried using $sp.getParameter() as a work-around and no change.
I would really appreciate any help or insight on what I can do different to get script to work and filter the right records.
If I understand your question correctly, you are asking how to get the sysId of the sysapproval_approver record from the client-side in a widget.
Unless you have defined current elsewhere in your server script, current is undefined. Secondly, $sp.getParameter() is used to retrieve URL parameters. So unless you've included the sysId as a URL parameter, that will not get you what you are looking for.
One pattern that I've used is to pass an object to the client after the initial query that gets the list of requests.
When you're ready to send input to the server from the client, you can add relevant information to the input object. See the simplified example below. For the sake of brevity, the code below does not include error handling.
// Client-side function
approveRequest = function(sysId) {
$scope.server.get({
action: "requestApproval",
sysId: sysId
})
.then(function(response) {
console.log("Request approved");
});
};
// Server-side
var requestGr = new GlideRecord();
requestGr.addQuery("SOME_QUERY");
requestGr.query(); // Retrieve initial list of requests to display in the template
data.requests = []; // Add array of requests to data object to be passed to the client via the controller
while(requestsGr.next()) {
data.requests.push({
"number": requestsGr.getValue("number");
"state" : requestsGr.getValue("state");
"sysId" : requestsGr.getValue("sys_id");
});
}
if(input && input.action=="acceptApproval") {
var sysapprovalGr = new GlideRecord('sysapproval_approver');
if(sysapprovalGr.get(input.sysId)) {
sysapprovalGr.setValue('state', 'Approved');
sysapprovalGr.setValue('approver', gs.getUserID());
sysapprovalGr.update();
gs.addInfoMessage("Accept Approval Processed");
}
...

How do I get a random beer every time I reload the page?

I'm trying to fetch a random beer and a list of 20 beers from the Punk Beer API , and display it on the page. However, for some reason, the API with the random url kept returning the same beer (id:221, name: Blitz Series).I'm confused why it's not a different beer each time I reload the page.
Here's my code :
componentDidMount(){
const root_api = "https://api.punkapi.com/v2/";
var self = this;
axios.all([
axios.get(`${root_api}/beers/random`),
axios.get(`${root_api}beers?page=2&per_page=20`)
])
.then(axios.spread(function (randomBeerResponse, beerListResponse) {
self.setState({randomBeer:randomBeerResponse.data[0]})
self.setState({beers:beerListResponse.data})
}));
}
You are adding const root_api = "https://api.punkapi.com/v2/"; to axios.get(${root_api}/beers/random) which creates "https://api.punkapi.com/v2//beers/random"
As you can see there are two slashes "//" between v2 and beers and that leads to returning the same beer over and over again.
Write axios.get(${root_api}beers/random) instead and the problem will resolve.
The whole code in correct format would be:
componentDidMount(){
const root_api = "https://api.punkapi.com/v2/";
var self = this;
axios.all([
axios.get(`${root_api}beers/random`),
axios.get(`${root_api}beers?page=2&per_page=20`)
])
.then(axios.spread(function (randomBeerResponse, beerListResponse) {
self.setState({randomBeer:randomBeerResponse.data[0]})
self.setState({beers:beerListResponse.data})
}));
}
As a side-note, when you're dealing with API, you should test the API outside of your program with an API testing/development tool like Postman.
The problem here has nothing to do with React. It's just the API.
Add a header in your axios request : headers: {"Pragma", "no-cache"};
This tells browser to make a new call everytime and not memorize the previous request.

Getting Parse Objects via pointers

I am trying to get a Reservation object which contains a pointer to Restaurant.
In Parse Cloud code, i am able to get the restaurants objects associated with Reservations via query.include('Restaurant') in log just before response.success. However, the Restaurants reverted back to pointer when i receive the response on client app.
I tried reverted JSSDK version to 1.4.2 & 1.6.7 as suggested in some answers, but it doesn't work for me.
Parse.Cloud.define('getreservationsforuser', function(request, response) {
var user = request.user
console.log(user)
var query = new Parse.Query('Reservations')
query.equalTo('User', user)
query.include('Restaurant')
query.find({
success : function(results) {
console.log(JSON.stringify(results))
response.success(results)
},
error : function (error) {
response.error(error)
}
})
})
response :
..."restaurant":{"__type":"Pointer",
"className":"Restaurants",
"objectId":"kIIYe7Z0tD"},...
You can't directly send the pointer objects back from cloud code even though you have included it. You need to manually copy the content of that pointer object to a javascript object. Like below:
var restaurant = {}
restaurant["id"] = YOUR_POINTER_OBJECT.id;
restaurant["createdAt"] = YOUR_POINTER_OBJECT.createdAt;
restaurant["custom_field"] = YOUR_POINTER_OBJECT.get("custom_field");
ps: in your code you seem do nothing else other than directly send the response back. I think parse REST api might be a better choice in that case.
It turned out that my code implementation was correct.

Parse.com - .get() is returning an [object Object]?

I have Posts and Topics on Parse. There are ~20 different Topics - each Post stores up to 5 of them in an array relationship.
When a Post is updated, I need to check the Topics it's assigned to and potentially send out notifications.
So I wrote this:
Parse.Cloud.afterSave("Post", function(request) {
Parse.Cloud.useMasterKey();
var postObject = request.object;
var postTitle = postObject.get("title");
var topics = postObject.get("topic");
var topicCount = topics.length;
console.log("Post with title " + postTitle + " has " + topicCount + " topics: " + topics);
// code continues to push logic,
// but already the log above is wrong, so I'll leave that off.
}
For example, if I make a post with a title of "Porsche Takes Aim at Tesla" and give it a single topic, the one for "tech", I would expect it to log something like:
Post with title Porsche Takes Aim at Tesla has 1 topics: tech
But instead when I do this, it logs:
Post with title Porsche Takes Aim at Tesla has undefined topics: [object Object]
What am I doing wrong? The documentation suggests that when you call get with the name of an array field, it should return a JavaScript array. Instead it seems to be returning a blank JavaScript object without any attributes or contents at all.
I just need some way of seeing which topics are attached and iterating through them.
Note that I know this object is being created properly because I can see it in the data browser just fine. Navigating to the Post and then clicking on View Relationship under topic shows me that it's properly connected to the tech topic.
First: [object Object] doesn't mean that object is empty. Use JSON.stringify() to see it's contents.
As I understand topic is an object. In this case you shouldn't expect it to be printed as "tech". I guess you meant some property of this topic, like "name".
There may be also problem with setting the topic. Make sure to always use something like postObject.set("topic", arrayWithTopicsInside) because I think that you have set this to topic that is not in an array. You may need to remove that column so it can be added with different type.
I think it should look like this:
Parse.Cloud.afterSave("Post", function(request) {
Parse.Cloud.useMasterKey();
var postObject = request.object;
var postTitle = postObject.get("title");
var topics = postObject.get("topic");
Parse.Object.fetchAll(topic).then(function(topics) {
var topicNames = []
_.each(topics, function(topic) {
var name = topic.get("name");
names.push(name);
});
console.log("Post with title " + postTitle + " has " + topicNames.count + " topics: " + topicNames);
}, function(error) {
concole.log("error fetching objects" + JSON.stringify(error));
});
}

Dynamic Websites on Parse

I am trying to figure out how Dynamic Websites on Parse work.
I have followed the instructions here: https://parse.com/docs/hosting_guide#webapp to set up a basic example.
Beside cloud/views/hello.ejs, I have made cloud/views/mything.ejs and used that from app.js and it all works.
Now I would like to show for example the number of records in MyClass on Parse.
In other words, inside my Dynamic Website I want to display information related to the contents of the DB on Parse.
How can I do that? Obviously I need to include some DB query at some point, but is there any sample?
Here's an example snippet
This handles a GET request to /mypage, on execution it creates a Parse Query but returns a result count instead of a record set matching the query. On completion of the query we then set a count or error on the response using res.set(). You can then use ejs to display the count or error.
app.get('/mypage', function(req, res) {
var query = new Parse.Query('My Class');
query.equalTo("name", "Joe Blogs");
query.count({
success: function(count) {
res.set('count', count);
},
error: function(error) {
res.set('error', error);
}
});
});

Resources