I'm using firebase / AngularJS, but I think this is a plain JavaScript object structure problem in my opinion.
Signing in Facebook manually with firebase, I cannot get access to 4 specific properties that are inside the "result" object from the signInWithCredential process.
(console) Logging the "result" object, the 4 variables are in there indeed, I just cannot get direct access to them.
However, I can get access to all the other properties of the object, which turns this into a really weird bug.
Here is the code :
firebase.auth().signInWithCredential(credential).then(function(result){
// $localStorage.firebaseToken = result.providerData.stsTokenManager.accessToken;
var chatUserData = {};
var updates = {};
console.log("result str obj : " + JSON.stringify(result)); // defined
console.log("result str > uid is " + JSON.stringify(result.uid)); // defined
console.log("result str > displayName is " + JSON.stringify(result.displayName)); // defined
console.log("result str > photoURL is " + JSON.stringify(result.photoURL)); // defined
console.log("result str > email is " + JSON.stringify(result.email)); // defined
console.log("result str > emailVerified is " + JSON.stringify(result.emailVerified)); // defined
console.log("result str > isAnonymous is " + JSON.stringify(result.isAnonymous)); // defined
console.log("result str Provider Data " + JSON.stringify(result.providerData)); // array of objects - defined
console.log("result str > apiKey is " + JSON.stringify(result.apiKey)); // gets undefined
console.log("result str > appName is " + JSON.stringify(result.appName)); // gets undefined
console.log("result str > authDomain is " + JSON.stringify(result.authDomain)); // gets undefined
console.log("result str > stsTokenManager is " + JSON.stringify(result.stsTokenManager)); // gets undefined
chatUserData.username = result.displayName;
chatUserData.email = $localStorage.fb_data.data.email;
chatUserData.id = result.uid;
updates['/users/' + result.uid + "/"] = chatUserData;
firebase.database().ref().update(updates);
}).catch(function(error) {
console.log("Error! " + JSON.stringify(error));
});
And here is the object code from the console log :
which makes this into a weird bug because I know the object is there, and supposedly I'm accessing it right, but it just doesn't load. This is usually just a stupid mistake I made, but I just can't seem to find it.
(the facebook profile I have there is fake)
Hope you guys can help, Cheers.
I fixed this, turns out this is a kind of firebase-managed object, the way to correctly loop through it is using angular.foreach. And even that way is not the correct one.
I should have used user.getToken(), and I found it right in the documentation.
Firebase User Get Token
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);
});
}
What I am trying to achieve is that whenever a new client is added to the clients array, I want the function to return "Welcome (new customer name), you are (his position in the array) in line."
What am I doing wrong? I am trying to get the index of the new client to add 1 so that it starts to count from 1.
let clients = ['Smith', 'Stebve', 'John']
function clientQue(array, newCustomer) {
array.splice(1, 0, newCustomer)
return "Welcome " + newCustomer + ", you are number " + parseInt(array.indexOf('newCustomer')) + 1 + " in line.";
}
clientQue(clients, 'Bob');
You are missing paranthesis () to break out of the string concatenation and do the math first
"Welcome " + newCustomer + ", you are number " + (array.indexOf(newCustomer) + 1 ) + " in line.";
Since you're using array.splice to insert at the 1st index position always, you could also remove all the array.indexOf and always simply output 1 though.
This will work for you (I always like using `` when concating strings
let clients = ['Smith', 'Stebve', 'John']
function clientQue(array, newCustomer) {
array.splice(1, 0, newCustomer)
return `Welcome ${newCustomer} you are number ${parseInt(array.indexOf(newCustomer)) + 1} in line.`;
}
let message = clientQue(clients, 'Bob');
console.log(message)
This is the output
Welcome Bob you are number 2 in line.
Remove the quotes in this part from newCustomer. Needs to be evaluated to 'Bob'.
parseInt(array.indexOf(newCustomer))
You could unshift the new client and take the retuend new lenght of the array as value.
function clientQue(array, newCustomer) {
return "Welcome " + newCustomer + ", you are number " + array.unshift(newCustomer) + " in line.";
}
let clients = ['Smith', 'Stebve', 'John']
console.log(clientQue(clients, 'Bob'));
let clients = ['Smith', 'Stebve', 'John']
function clientQue(array, newCustomer) {
return "Welcome " + newCustomer + ", you are number " + (parseInt([...array, newCustomer].indexOf(newCustomer)) + 1) + " in line.";
}
clientQue(clients, 'Bob');
This should return what you need, however Array.splice mutates the original array, which means it also modifies the values of the original array. It can be harmful to change values of the given parameter.
[...array]
creates a new array from the old array's elements and
[...array, newCustomer]
pushes the new element inside the new array.
Reposting my answer since it was deleted and I wasn't given a chance to update it
A couple things:
Calling .splice removes an element from the array, and based on your question it doesn't seem like that is the desired result.
Furthermore, you don't need to call array.indexOf() because the length of the array will be the position of the newly added client.
You can have a function that takes a name such as "Jeff", adds it to the client array, and then returns the welcome message. Here is an example.
var clients = ["bob", "jane", "isaac", "harry"];
function addClient(name) {
clients.push(name);
return "Welcome " + name + " you are position " + clients.length + " in array";
}
console.log(addClient("Jeff"));
As clients is a queue, I think you would like to put the new customer to the front of the array, so you should use index 0 in splice. i.e.
array.splice(0, 0, newCustomer);
If you would like to put the newCustomer at the back, you may use push() instead. (I prefer using push() actually.)
array.indexOf() should return a number, so you do not need to use parseInt().
However, as you are using string operations before incrementing the index, you should use parenthesis for the addition operation. i.e.
'Welcome ' +
newCustomer +
', you are number ' +
(array.indexOf(newCustomer) + 1) +
' in line.'
Note: As Bob is the new customer at the end of the queue, you may need to change the index calculation to: (array.length - array.indexOf(newCustomer)) instead.
Let's put them together,
let clients = ['Smith', 'Stebve', 'John'];
function clientQue(array, newCustomer) {
array.splice(0, 0, newCustomer);
return (
'Welcome ' +
newCustomer +
', you are number ' +
(array.indexOf(newCustomer) + 1) +
' in line.'
);
}
const q = clientQue(clients, 'Bob');
console.log(q); // Welcome Bob, you are number 1 in line.
console.log(clients); // [ 'Bob', 'Smith', 'Stebve', 'John' ]
I am writing a script to take a stock number, loop through existing stock numbers until a match is NOT found, then assign that unique stock number to the record. My problem is that the usual data[i][2] doesn't seem to reference a 'query' the same way that Apps Script would reference an array.
Fair warning, I'm trying to expand my Apps Script skills in to broader Javascript so I there's a good chance I'm doing it all wrong - I'm all ears if you tell me I'm doing this all incorrectly!
Using the log: data[i][2] gives me 'undefined' whereas data[2] gives me all fields of the third item in my query. Based on this I feel like I just need to learn how to reference it properly.
//Querying my datasource as 'var data'
var query = app.models.UsedVehicles.newQuery();
query.filters.ParentDealType._contains = prefix;
var data = query.run();
//Returns four records which is correct.
var testStockNo = prefix+month+countstring+year;
console.log("Test Stock Number " + j + ": " + testStockNo);
for (i = 0; i < data.length; i++){
console.log("data[i][2]: " + data[i][2]); //results: undefined
console.log("data[2]: " + data[2]); //results: all fields of 3rd query result.
if(data[i][2] === testStockNo){
k++;
break;
}else{
console.log("No Match");
}
}
Even if testStockNo equals the value in field:TStockNo, the log displays:
Test Stock Number 1: C1200118
data[i][2]: undefined
data[2]: Record : { TIndex: 8, TVin8: HS654987, TStockNo: null,
TParentStkNo: GSD6578, TYear: 2010, TMake: NISSAN, TModel: PICKUP,
TMileage: 24356, ParentDealType: C}
No Match
Issue/Solution:
query.run() returns array of records and NOT a array of arrays(2D). You should access the Record value using it's key instead of a index.
Snippets:
console.log("data[i][TStockNo]: " + data[i]['TStockNo']);
console.log("data[i].TStockNo: " + data[i].TStockNo);
console.log("data[2]: " + data[2]);
References:
Query#Run
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'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.