I've been trying to get the contact name and last name and also get the cellphone number but when I am using the getAll() method the Console display this:
Found: Daniel Garcia [object Object]
As you can see it displays the Name + LastName + tel. Why does it display tel like [object Object]
Here is my code:
var allContacts = navigator.mozContacts.getAll({
sortBy: "givenName",
sortOrder: "ascending"
});
allContacts.onsuccess = function(event) {
if (cursor.result) {
if (cursor.result.familyName[0]== undefined) {
cursor.result.familyName[0]= "";
}
console.log("Found: " + cursor.result.givenName[0] + " " + cursor.result.familyName[0] + ' ' + cursor.result.tel[0]);
cursor.continue();
} else {
console.log("No more contacts");
}
}
tel here is an array of objects - a list of all the possible phone numbers for the contact. Each of this objects has several useful properties. In javascript when you are printing out an object it prints an object string representation (like [object Object]).
Check out docs to understand the structure of the tel object and get it printed the way you want: https://developer.mozilla.org/en-US/docs/Web/API/mozContact.tel
Thank you very much it work just like Aras and jamesh proposed
console.log("Found: " + cursor.result.givenName[0] + " " + cursor.result.familyName[0]+' '+JSON.stringify(cursor.result.tel[0]));
and the console display this:
"Found: Daniel Garcia {"type":["mobile"],"value":"8112441018"}"
Related
I'm a PHP coder so not fully in the know with Javascript so possibly making a real schoolboy error here. I'm trying to facilitate some client side DB using Dexie & IndexDB. I can get values in using db.table.put and retrieve them all using db.table.toArray. However I seem to be struggling with getting individual records back out again. I'm trying to query for Keydata based on a form input. Heres my code, but it returns a TypeError cannot read property of undefined.
Here I can confirm in line 4 that my 'search2' variable contacts keyData (e.g. 1). But when used in line 5 it doesn't return a valid result. Can anyone see the issue?
function searchData(){
var search2;
search2 = document.getElementById('search2').value;
alert ("Id " + search2);
db.contacts.get(search2).then
(function (contact) {
alert ("Id " + contact.id + " : " + contact.title + " " + contact.forename + " " + contact.surname);
}).catch(function(error) {
alert ("Ooops: " + error);
});
}
I use Google plus API and get this data from it and get an error while parsing this JSON data
and here is the code I use to parse this data and get error as data object is tested and work fine and hold data as it appear in past 2 images
var allIems = data.items;
for (var element in allIems) {
document.getElementById('datafromapi').innerHTML +=
(" , published time :" + element.published +
" , last updated time :" + element.updated +
", complete url : " + element.url
);
var obj = element.object.attachments;
document.getElementById('datafromapi').innerHTML +=
(+"\nattachments of post :\n" +
" type : " +
obj[0].objectType +
" ,displayName of content : " +
obj[0].displayName +
" ,content URL : " +
obj[0].url +
" ,content data :" +
obj[0].image.url +
" ,content type : " +
obj[0].image.type +
" ,content height : " +
obj[0].image.height +
" ,content width : " +
obj[0].image.width +
"\n\n\n");
}
});
i got that error appear
Uncaught TypeError: Cannot read property 'attachments' of undefined
element values in
for (var element in allIems) {
are keys of allItems, which in this case are array indices. You have to address the actual array items like this:
var obj = allItems[element].object.attachments;
Your code element.object.attachments; tries to access property object of a number, which does not exist.
Since we know that allItems is an array, you could have written:
for (var i = 0; i < allIems.length; i++) {
var obj = allItems[i].object.attachments;
Javascript has a built in JSON parser you can use that takes in a string of the data and returns an object.
let jsonDataAsString = "{\"a\":1,\"b\":2}";
let jsonDataAsObject = JSON.parse(jsonDataAsString);
Then you can traverse through the data as an object, referencing properties using dot-notation
console.log(jsonDataAsObject.a); // 1
To be safe, you should be comparing properties to null before trying to use then
if(jsonDataAsObject.somePropery != null) {
// the property exists so you can access it here
}
I have the following lines of JS in a program...
console.log(self.dataset[altAspect][altGroup] + " is the contents of the altGroup");
console.log(answer + " is the answer to be checked");
console.log('it is ' + (self.dataset[altAspect][altGroup].indexOf(answer) > -1) + ' that ' + answer + ' is a member of ' + self.dataset[altAspect][altGroup]);
if (!self.dataset[altAspect][altGroup].indexOf(answer) > -1){
self.wrongGroups.push(altGroup);
console.log('added ' + altGroup + ' to the wrong groups!');
self.altCount++;
}
this logs the following to the console:
ginger,daisy is the contents of the altGroup app.js:203:21
daisy is the answer to be checked app.js:204:21
it is false that daisy is a member of ginger,daisy app.js:205:21
added skinny to the wrong groups! app.js:208:25
My question is, why is the above saying that "daisy" is not a member of ["ginger", "daisy"]? Obviously when I run [ "ginger", "daisy" ].indexOf("daisy") I should get 1 in return.
if you use [ "ginger", "daisy" ].indexOf("daisy") you got 1 as index value and
if you use this [ "ginger", "daisy" ].indexOf(answer) you getting -1 as index value..
so, it may arises due to some whitespace may occur in your variable answer
you try to compare length of both...
compare answer.length with "daisy".length or else try this,
[ "ginger", "daisy" ].indexOf(answer.trim()) . probably you will notice issue while getting text length..
I'm a total JS beginner. Here is the JsBin link formLetter test should be passing.
TL;DR
This:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
console.log(formLetter("Hermione", "Luna","How are you?"));
Should return:
"Hello Hermione,
How are you?
Sincerely,
Luna"
But instead I get this:
"Hello [object Object],
undefined
Sincerely,
undefined"
Edit
Sorry for the confusion. I'm working on different problems inside one JsBin. This is the correct JsBin with the isolated code.
This is because you are only getting one object passed into the function call. This object contains the information you need in lieu of the named arugments you have provided.
The first argument, recipient being [object Object] tells you that it's an object. undefined means that nothing was passed in their place. This signifies the common pattern of a config or param object being passed to the function call. Because of this, what you have as named arguments should really be property look ups on the object provided as the first argument.
Your function definition should look more like:
var formLetter = function (letter) {
// do something with letter
};
Inside of that function call, you may then hit the properties of the letter object to see if they contain what you need, Doing console.log debugging in dev tools will help track it down.
The line:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
in your example needs one semicolon after "sender", like:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender;
};
Your undefined is related to the use of anidated console.log.
You do:
console.log(longMessage.formLetter("Hermione", "Luna","How are you?"));
and (in the JsBin) you have also:
var longMessage = {
formLetter: function(recipient, sender, msg) {
console.log("Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender);
}
};
In the example of your question you have them corrected.
Double check the code you post, please.
After looking at your test in jsbin, I noticed that in your assert.deepEqual() method you run formLetter(letter) and compares it to the concatenated string you created.
The problem is that formLetter() expects three string values and you send it an object (letter). That's why you get [Object object] in the first location and undefined in the others.
You should run formLetter(letter.recipient, letter.msg, letter.sender) in your assert and it should work properly.
I'm working on a twitch bot in node.js and using tmi.js https://github.com/Schmoopiie/tmi.js
My problem is with the logging system I created. I think I'm misunderstanding node.js from the ground up here. I need to get the username from the chatter that just typed something.
This is my code:
client.on('chat', function (channel, user, message, self) {
fs.open('logs/' + user +'.txt','r',function(err,fd){
if (err && err.code=='ENOENT')
{
fs.writeFile('logs/' + user +'.txt', '[' + 'GMT' + moment().format('Z ') + moment().format('D.M.YYYY H:mm:ss') + '] ' + user + ': ' + message, function (err) {});
} else {
fs.appendFile('logs/' + user +'.txt', '[' + 'GMT' + moment().format('Z ') + moment().format('D.M.YYYY H:mm:ss') + '] ' + user + ': ' + message + '\n', function(){});
}
});
});
this triggers everytime a user types a message in chat. And I thought the variable "user" would have the value of the chatter that just typed something.
But the value of user is always [object Object]
Am I overlooking something? I can't find a way to get the username of the current chatter.
Thanks for the help guys
I can't say I have any experience with tmi.js, but it appears the user variable you're getting there is an object, not a string. When you try to cast it to a string using +, you get [object Object] by default. Try doing this:
fs.appendFile('logs/' + JSON.stringify(user) +'.txt', '[' + 'GMT' + moment().format('Z ') + moment().format('D.M.YYYY H:mm:ss') + '] ' + JSON.stringify(user) + ': ' + message + '\n', function(){});
or, even better, because you want that just for debugging:
console.log(user);
And check the console. It probably won't be pretty, but at least you'll be able to see what's inside the user variable. Perhaps it will be an object with a username property, and you'll be able to simply use user.username?