Hey friends i am using javascript sdk to post on users friends wall with jQuery facebook multi friend selector however i am getting this error friendId.split is not a function. Here is my code
function remendToFriend(pic, url, friendId, fromName)
{
alert(friendId);
var friendList ;
pFriend = new Array();
pFriend = friendId.split(',');
for( x in pFriend )
{
alert(pFriend[x]);
var publish = {
method:'feed',
picture:pic,
link:url,
name:'SHARP Product Remend',
caption: fromName + 'has remend a product to you via Sharp Expert lounge',
};
FB.api('/'+pFriend[x]+'/feed', 'post', publish, function(resp) {
if( !response || response.error )
alert('Unable to share');
else
alert('Successfully posted to firends wall');
});
}
}
In alert box i got ma seperated friend ids so i use split function post on each users wall seperately i dont know whats wrong here please help me
Hey friends i am using javascript sdk to post on users friends wall with jQuery facebook multi friend selector however i am getting this error friendId.split is not a function. Here is my code
function remendToFriend(pic, url, friendId, fromName)
{
alert(friendId);
var friendList ;
pFriend = new Array();
pFriend = friendId.split(',');
for( x in pFriend )
{
alert(pFriend[x]);
var publish = {
method:'feed',
picture:pic,
link:url,
name:'SHARP Product Remend',
caption: fromName + 'has remend a product to you via Sharp Expert lounge',
};
FB.api('/'+pFriend[x]+'/feed', 'post', publish, function(resp) {
if( !response || response.error )
alert('Unable to share');
else
alert('Successfully posted to firends wall');
});
}
}
In alert box i got ma seperated friend ids so i use split function post on each users wall seperately i dont know whats wrong here please help me
Share Improve this question asked May 14, 2012 at 12:11 Code PrankCode Prank 4,2506 gold badges33 silver badges48 bronze badges 6-
6
Most probably this is because
friendId
is not a string but an array. – VisioN Commented May 14, 2012 at 12:12 -
7
What does
console.dir(friendId)
orconsole.log(typeof friendId)
tell you?alert
is the worse debugging method you could have possible chosen. – Felix Kling Commented May 14, 2012 at 12:14 - 2 console.log(typeof(friendId)) returns object – Code Prank Commented May 14, 2012 at 12:23
- 1 Then you should address a certain property or iterate it to get the values. – VisioN Commented May 14, 2012 at 12:24
- how can i iterate through in objects like we use forin loop for arrays is there any equivalent for objects? – Code Prank Commented May 14, 2012 at 12:30
3 Answers
Reset to default 7Most probably friendID
is already an array. If you call alert
the array is converted to a string and bees a ma separated list of values.
Note that converting an array to a string is not the same as calling JSON.stringify
(where you get also brackets and double quotes around elements when they're strings)
The JavaScript split()
function is for the type string ie for eg.
var friendid='1,34,67';
As VisioN says, when you alert an array you get ma separated values.
You can traverse JS Objects like this
for (var key in friendid) {
var obj = friendid[key];
for (var prop in obj) {
alert(prop + " = " + obj[prop]);
}
}
Hope this helps
alternate
for( var x in friendId )
{
alert(friendId[x]); // this would be your desired value
}