Cannot read property 'employee_name' of undefined - api

Im using Postman platform for testing API. I'm actually new to Postman. So, Currently testing dummy APIs, where i fetched the GET response result from the URI
http://dummy.restapiexample.com/api/v1/employees , when i add validation points :
specifying test description as "name" See below
var response = JSON.parse(responseBody);
tests["name"] = response.data[2].employee_name == "Ashton Cox";
Test operation successfully passed. But when using specific id in the URI link
http://dummy.restapiexample.com/api/v1/employee/5
with same validations points as above, i'm getting response as TypeError: Cannot read property 'employee_name' of undefined, below is the screenshot

The correct reference and syntax would look like this:
pm.test("Check User", () => {
const responseJson = pm.response.json();
pm.expect(responseJson.data.employee_name).to.eql("Airi Satou");
});

Related

DataTables: Cannot read property 'length' of undefined, but the JSON is valid

var deptTable = $('#team-table').DataTable( {
"ajax": "/cc/AjaxDashboard/getMyTeam",
"pageLength": 10,
"scrollY": "250px"
});
I have this. The JSON response is just [] as there's no data. I am getting:
Cannot read property 'length' of undefined
I thought [] was valid JSON as per https://jsonlint.com/ and it would show an empty message but I get an error instead
[] is indeed a valid JSON syntax for empty array. The reason you're getting the error is because the data returned from your AJAX doesn't match the DataTable's default expectation.
There are 3 ways you can use with the ajax option:
Give it a string as the AJAX source
Give it a configuration object
Give it a function
And by default, when the AJAX source is given, DataTables expects the returned data to be an array of items with the key data:
{
data: []
}
If your AJAX doesn't return this format, you will get that error. Here is the proof: https://jsfiddle.net/davidliang2008/s2xgof6t/22/
To fix it, you can override the returned format by setting dataSrc to an empty string, like this:
{
ajax: {
url: xxx,
dataSrc: ''
}
}
demo: https://jsfiddle.net/davidliang2008/s2xgof6t/26/
More reads on this custom data source property: https://datatables.net/examples/ajax/custom_data_property.html

How to update the snippet.description on Youtube from Google Apps Script API using only the videoId?

I want to update my snippet.description on my Youtube channel using the API exposed through Google Apps Script. I know the videoId already so there is no need to create and loop through a search list like they show in the example here.
I expect to see my script complete with the new description on my Youtube video. But I get the following error message instead.
API call to youtube.videos.update failed with error: Forbidden (line 90, file "Youtube")
Code.gs
function updateVideo( data ) {
var videoId = 'foo';
var title = 'bar';
var description = 'baz';
var resource = {
id: videoId,
snippet: {
title: title,
description: description,
categoryId: '22'
}
};
YouTube.Videos.update(resource, 'id,snippet'); // this is line 90
}
What am I doing wrong?
Note
To clarify: By "only the videoId", I mean without creating a list of channels to find it like they do here.
var myChannels = YouTube.Channels.list('contentDetails', {mine: true});
Edit
This question is different because the other asks specifically about HTML. This question is not about using HTML in the description. It's about making any changes whatsoever to the description.
Summary from comments:
This is an authorization error. The account making the API request was not the same account as the owner of the YouTube video.

Component method response object data binding

I am starting to lose my mind in debugging an application that I inherited from a fellow developer who is absent.
I have narrowed down the problem to the following place in code (php files are checked, Vue instances are initialised, there are no syntax errors).
This is my the component that gets initialised:
var RadniStol = Vue.component('radnistol', {
template: '#template-RadniStol',
data() {
return {
tableData: [],
requestData: {
sort: "ID",
order: "DESC"
}
}
},
methods: {
reloadTable: function (event) {
data = this.requestData;
this.$http.post('php/get/radni_stol.php', data).then(response => {
console.log(response.data.bodyText);
this.tableData = response.data.records;
});
},
.
.
.
The PHP file that gets called with the POST method is working correctly, querying the database and echoing the response in a JSON format.
The thing that is making me pull out my hair is the following: the console.log(response.data) outputs the following into the console:
{"records":[{"DODAN_NA_RADNI_STOL":"1","..."}]}
It is an JSON object that I expected to have but when trying to assign it to the data of the component with:
this.tableData = response.data;
or any other way… response.data.records returns ‘undefined’ in the console. I have tryed with JSON.parse() but no success.
When logging types to console:
response variable is a response object with a status 200 and body and bodyText containing the data from the database.
response.data is a string type containing the string JSON with the data from the database.
When trying to use JSON.parse(response.data) or JSON.parse() on anything in the callback of the POST method I get the following error in the console:
RadniStol.js?version=0.1.1:17 Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
at VueComponent.$http.post.then.response (RadniStol.js?version=0.1.1:17)
at <anonymous>
I am really starting to lose my mind over this issue, please help!
Thank you
If response.data is string, with JSON inside, then to access the records field, you should decode it like this:
JSON.parse(response.data).records
Not sure this has something to do with PHP or Vue.js, it is just plain javascript issue.
If it not decodes, than problem is definitely in response.data. For example
{"records":[{"DODAN_NA_RADNI_STOL":"1","..."}]}
is not a valid JSON, because key "..." needs to have some value.
But it seems to me that response.data is already parsed.
What I suggest you to do, is to write handler of the response as separate function, make response object that mimics actual response object by hand, and then test it separately from request. So you could show us request object and function that works with it.
I had the same error and fixed it.
Result will be response.body not response.data.
Here is my code:
getS: function(page) {
this.$http.get('vue-manager?page=' + page).then((response) => {
var data = JSON.parse(response.body);
this.student = data.data.data;
this.pagination = data.pagination;
});
},

Get tagged photo without access token

I have some problem on this. Can I get public tagged photo from Instagram api without getting any code or access token?
Please share any link for reading because I cannot found any. I feel it is less knowledge about Instagram api on web.
Thanks!
You can pull public media by tag without authentication.
Take a look at the API documentation for the get /tags/tag-name/media/recent endpoint. Here's the URL: http://instagram.com/developer/endpoints/tags/#get_tags_media_recent
The documentation can be confusing, it shows using an access_token for this endpoint in the example, but it is not required. You will need to register an application and get a client ID.
I use MeteorJS and call a method server side that returns essentially the 'view source' of the instagram page. So if you can run a server side scrape on the tag url you will be able to handle the response with what i have below and it will push all the images into an array.
//server side method
Meteor.methods({
'scrapeInst':function(tag){
return Scrape.url('https://www.instagram.com/explore/tags/'+tag+'/')
}})
//client side logic
Meteor.call('scrapeInst',Session.get('params').tag,function(err,resp){
var theInstResp = resp;
cleanOne = resp.replace(/>|window._sharedData = |;</|;|#47;|<|/g,'').split('script')
var splitter = cleanOne[22].split(',');
var theArr = [];
_.each(splitter,function(e){
var theFinal = {};
var theS = e.split(":");
if(theS[0].replace(/"| |/g,'') === "display_src"){
theFinal[theS[0].replace(/"| |/g,'')] = theS[2].replace(/%22/g,'');
theArr.push(theFinal)
}
});
Session.set('photos',theArr);
setTimeout(function(){
Session.set('loading',false)
},1000)
})

How to get the api returned data from a collection?

I have the following collection that gets data from an api:
var Data = Backbone.Collection.extend({
url: '/* api url - working */',
sync: function(method, model, options) {
options.timeout = 10000;
options.dataType = "jsonp";
return Backbone.sync(method, model, options);
}
});
And I want to display it in the view:
var MyView = Backbone.View.extend({
el : '.myview',
render : function () {
var data = new Data();
var that = this;
data.fetch({
success : function (data) {
console.log(arguments);
console.log(data);
var template = _.template( $('#temp').html(), {data: data.models} );
that.$el.html(template);
}
});
}
});
What I don't understand is that when I log arguments I get the api data and it has a wieired structure: {0:child, 1:object\* contains the api data *\, 2:object} but when I log data I get only the child
I don't understand this structure, what am I missing and how do I get the api data out of it?
Collection.fetch is calling the success function you passed in with 3 arguments (from Backbone source):
success(collection, resp, options);
arguments is a special local variable in all javascript functions. Check out this documentation.
So, logging arguments is showing the 3 things passed into the success function while logging data is showing just the first thing passed in. Even though your function only has 1 parameter, three things were still passed to it.
The first thing passed to success is the Backbone Collection itself. So, what you need to do with it depends on what your template looks like. It is common to pass just the JSON to the template function: _.template( $('#temp').html(), {data: data.toJSON()} ). Then in the template, you would need to iterate over those JSON objects.
<% _.each(data, function(item){ %>
<div><%= item.foo %></div>
<% }); %>