I have this function that iterates through a user's favourited track genres on SoundCloud, and then I want to push these results into an array however am having problems. Any suggestions?
SC.connect(function() {
SC.get('/me/favorites', function(favorites) {
console.log(favorites)
for (i = 0; i < favorites.length; i++) {
console.log(favorites[i].genre);
favorites[i].genre.push(meGenrePref);
}
var meGenrePref = [];
console.log(meGenrePref);
});
});
I'm not expert on SoundCloud APIs but watching your code I thought this solution:
var meGenrePref = [];
SC.connect(function(){
SC.get('/me/favorites',function(favorites) {
console.log(favorites);
for(var i = 0; i < favorites.length; i++)
{
console.log(favorites[i].genre);
meGenrePref.push(favorites[i].genre);
}
console.log(meGenrePref);
});
});
You were trying to push the contents to a wrong variable, which was the response from the API.
In this solution we are saving to meGenrePref all genres.
Related
I'm making an file managing web page with nodejs and need some help.
I'm adding some informations into JSON object which recieved from DB.
the DB constains stage informations and I should link informations about stage files.
this is code for the function. Added comments into codes.
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
// this is a problem. I will add file information for each file.
// I
for (var j = 0; j < data.stage[i].packFileNameArray.length; j++) {
fs.stat(dirPath + data.stage[i].packFileNameArray[j], function (err, fileStat) {
if (err) {
// this was first try. I thought the array will be automaticallt created.
data.stage[i].isExist[j] = 'NO';
data.stage[i].mTime[j] = '0';
data.stage[i].size[j] = '0';
} else {
// this was second try. I tried push into each time.
data.stage[i].isExist.push('YES');
data.stage[i].mTime.push(fileStat.mTime);
data.stage[i].size.push(fileStat.size);
}
console.log(data.stage[i].isExist[j]);
console.log(data.stage[i].mTime[j]);
console.log(data.stage[i].size[j]);
});
}
}
});
I want to know how I can add additional informations as an array into the JSON object.
Thank you.
data.stage[i].push({ isExist: 'Yes'});
etc
You have to create the arrays (as the do not seem to exist) before you can push data into them:
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
data.stage[i].isExist = [];
data.stage[i].mTime = [];
data.stage[i].size = [];
//...
I am trying to render an html page that contains all of the posts that a user has received. Right now the issue I am having (shown under Way 1) is that when I call the function renderPosts after the web socket is received, only the first post in the array is rendered (the array has more than one post in it).
On the other hand, Way 2 in which I have no for loop and instead manually render each post works in that all four posts are rendered. But I need to be able to work with an arbitrary number of posts which is why I need to use the for loop.
I am using socket.io and javascript.
Way 1:
socket.on('postsToRender', function(arrayOfPostsToRender) {
renderPosts(arrayOfPostsToRender);
});
function renderPosts(arrayOfPostsToRender) {
for (var index = 0; index < arrayOfPostsToRender.length; index++) {
renderPost(arrayOfPostsToRender[index]);
}
}
function renderPost(postToRender) {
var feed = document.getElementById("feed");
var postContent = document.createTextNode(postToRender.content);
var post = document.createElement("div");
post.appendChild(postContent);
feed.appendChild(post);
}
Way 2:
socket.on('postsToRender', function(arrayOfPostsToRender) {
renderPost(arrayOfPostsToRender[0]);
renderPost(arrayOfPostsToRender[1]);
renderPost(arrayOfPostsToRender[2]);
renderPost(arrayOfPostsToRender[3]);
});
function renderPost(postToRender) {
var feed = document.getElementById("feed");
var postContent = document.createTextNode(postToRender.content);
var post = document.createElement("div");
post.appendChild(postContent);
feed.appendChild(post);
}
Try this:
function renderPosts(arrayOfPostsToRender) {
for (var index = 0; index < arrayOfPostsToRender.length; index++) {
(function(i){
renderPost(arrayOfPostsToRender[i]);
})(index);
}
}
I have this code:
rtclient.listFiles = function(callback) {
gapi.client.load('drive', 'v2', function() {
gapi.client.drive.files.list({
'maxResults': 20
}).execute(callback);
});
}
which I try to assign to an array using this code:
var arrayStore;
rtclient.listFiles(function(x) {
for(var i in x.items) {
arrayStore.push(x.items[i].id);
}})
However, the array arrayStore is not storing anything. This is using the Google Drive API and I am trying to access some file IDs to iterate over. Can anyone help?
the point is you should have a error like:
TypeError: Cannot call method 'push' of undefined
because you haven't defined it as an array, the other point is don't use for(...in) for arrays, you can find reasons about that in this post:
Why is using “for…in” with array iteration such a bad idea?
var arrayStore = [];
rtclient.listFiles(function(x) {
for(var i = 0; i < x.items.length; i++) {
arrayStore.push(x.items[i].id);
}
});
The other important point as #toothbrush has commented is, since the Google Drive API is asynchronous, you can't access the contents of arrayStore immediately, in other word to continue your scenario you should do something like this:
var arrayStore = [];
rtclient.listFiles(function(x) {
for(var i = 0; i < x.items.length; i++) {
arrayStore.push(x.items[i].id);
}
nextStep();
});
function nextStep(){
//here you would have access to arrayStore
console.log(arrayStore);
}
I'm trying to list all the playlists (and their tracks) of the current user on a Spotify integrated app using the API as explains in https://github.com/spotify/apps-tutorial/tree/1.0 .
Is there any way to do this? Or is not supported by now?
Using the 1.X API I did this:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
});
(From: https://developer.spotify.com/docs/apps/api/1.0/library_.html)
I suppose that my object returnedLibrary has all the playlists for the current user, isn't it? My problem is that I don't know what to do with this returned object and how to iterate through it.
Anyone knows how to access this information?
Thanks,
If you are interested in the user's playlists, you can do it like this:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
returnedLibrary.playlists.snapshot().done(function(snapshot) {
for (var i = 0, l = snapshot.length; i < l; i++) {
var playlist = snapshot.get(i);
// do something with playlist
}
});
});
Finally I have achieved my purpose:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
returnedLibrary.playlists.snapshot().done(function(snapshot) {
for (var i = 0, l = snapshot.length; i < l; i++) {
var playlist = snapshot.get(i);
aplay._collections();
aplay.tracks.snapshot().done(function(s){
selectedPlaylist = s;
for(k=0;k<s.length;k++){
s.get(k).name; //Will be the name of that song
}
});
}
});
});
Nevertheless, I don't know why I have to call aplay._collections(); (it seems is a private method). If I not do this call the tracks property is not set... Any ideas about this?
Thanks!
I am using Lawnchair for HTML5 iOS App Development for Persistant Storage.
I have an iteration script that returns undefined 11 times, here is the script
var lawnchair = Lawnchair({name:'relaxed', record:'config'}, function(people){
this.all(function(record, index) {
records = record.length;
couchURL = record.couchURL;
alert(records);
for(i = 0, len = records; i < len; i++){
console.log(couchURL);
}
})
})
The Data is obviously in local storage but isn't being iterated out correctly, could anybody provide a fix for this?
Thanks
Try this :
var lawnchair = Lawnchair({name:'relaxed', adapter:'dom'}, function(people){
})
this.all(function(data) {
for(var i = 0; i<data.length;i++)
{
Get data using this...data[i].value.fieldname
}
})