I have a block of code that updates document data
const { serverTimestamp } = firebase.firestore.FieldValue;
// The function firebase.firestore.Timestamp.fromDate(data.dob) seems to produce this behaviour
const fsData = {
...
dob: firebase.firestore.Timestamp.fromDate(data.dob),
doj: firebase.firestore.Timestamp.fromDate(data.doj),
...
}
try {
console.log(`fsData`, fsData)
await db.collection(FIRESTORE_COLLECTIONS.CLIENT_COLLECTION).doc(clientId).update({
...fsData,
updatedAt: serverTimestamp(),
});
console.log(`fsData`, fsData)
return true
} catch (e) {
console.log(`e`, e)
}
After the call to this function, It executes up until first console.log(fsData, fsData)
Then nothing happens, There is no response or error messages coming up. Then after a few minutes...
After experimenting, It was found that firebase.firestore.Timestamp.fromDate(data.dob) is causing this. Because, whenever i remove those two data, it was working.
The data that's being passed to this function Date type, This works well with creating documents but has issues when updating.
and here's the object right before passing to update
fsData
Object { doj: {…}, dob: {…}, firstName: "Amaya", lastName: "Gilbert", address: "Dolor quaerat quos l", cycle: "24", phone: "+123123123123" }
address: "Dolor quaerat quos l"
cycle: "24"
dob: Object { seconds: 1626449262, nanoseconds: 0 }
doj: Object { seconds: 1626535662, nanoseconds: 0 }
firstName: "Amaya"
lastName: "Gilbert"
phone: "+123123132123"
<prototype>: Object { … }
clientContext.tsx:128:20
#firebase/firestore: Firestore (8.7.0): Connection WebChannel transport errored: appears in console as warning.
Other functions, create and delete works well.
Related
I need to solve the error that you see on the screen, I already tried with .map and with foreach, even so my object is not traversed, I am clear that .map is only for Arrays but anyway I tried, An example of what I try to go through is in the console.
here a example of my object
{
OKaY30PcZOXBZngSLX33:
{
birthDate: U {seconds: 1622469668, nanoseconds: 156000000}
dni: "27347106"
email: "alew140#gmail.com"
firstName: "Alejandro"
gender: 0
lastName: "Pereira"
phone: "04144117131"
specialty: ""
}
UuynMRMJ9G85Ty9Ayln0:
{
birthDate: U {seconds: 1622469780, nanoseconds: 932000000}
dni: "27347106"
email: "alew140#gmail.com"
firstName: "Alejandro"
gender: 0
lastName: "Pereira"
phone: "04144117131"
specialty: ""
}
}
forEach works with Array, not objects.
I am trying to update my state with the new following array, however Even though the console shows no errors and prints out the new array correctly as it should be formed, the state does not update to reflect this.
What am I doing wrong?
state = {
notes: [
{
text: "mow the lawn",
author: "dean",
time: "10am"
},
{
text: "feed the dog",
author: "sam",
time: "2pm"
}
]
};
updateNotes = (title, name, eventTime) => {
let newNote = {
text: title,
author: name,
time: eventTime
};
this.setState(state => {
const list = state.notes.concat(newNote);
console.log(list); //shows the list formed correctly in the console
return {
list
};
});
};
Also for reference here is what I see in the console after running the code assuming I input the values, new text, some author, and 3pm when creating my new note
(3) [{…}, {…}, {…}]
0: {text: "mow the lawn", author: "dean", time: "10am"}
1: {text: "feed the dog", author: "sam", time: "2pm"}
2: {text: "new text", author: "some author", time: "3pm"}
It should be:
return {
notes: list
};
What you are doing now is adding a new variable list to the state.
Why do you update a field that is called list, even if your notes are inside field called notes?
{ list } is a shorthand to create an object with field called list and value that is stored in variable named in the same way.
Just update the proper field:
return {
notes: list,
};
I am using a combination of filter, some, and includes to return a filtered set of documents in my MongoDB/Node back-end environment.
While I can get this to work in a mock example, when I plug it in to my actual data, I get an error.
This is the key problematic piece of code:
let filteredDocs = docs.filter(doc => doc.branches._id.some(branch => mongoArrBranchID.includes(branch._id)));
When I console.log this out with:
console.log("filteredDocs: ", filteredDocs);
I get:
Reason: TypeError: Cannot read property 'some' of undefined
I've been scratching my head trying to figure out what the issue is here. Why is my mock example working, but not this?
One thought I had was that maybe the issue is that the comparison is made with different types. So then I checked with these two lines of code to make sure the comparison is using Mongo ObjectIDs in both cases (both return true):
console.log("is param value valid: ", mongoose.Types.ObjectId.isValid(mongoArrBranchID[0])); // returns true
console.log("is doc value valid: ", mongoose.Types.ObjectId.isValid(docs[0].branches[0]._id)); // returns true
So why am I getting the TypeError: Cannot read property 'some' of undefined error here?
By the way, just so you know what the data looks like, my passed-in filter values when consoled out look like this :
console.log("mongoArrBranchID: ", mongoArrBranchID); // result below
mongoArrBranchID: [ 5ac26645121f0613be08185d, 5ac26645121f0613be08185a ]
And again, this check returns true:
console.log("is param value valid: ", mongoose.Types.ObjectId.isValid(mongoArrBranchID[0])); // returns true
My docs data looks like this when I console out the first of the docs:
console.log("doc branches: ", docs[0].branches); // result below
doc branches: [{"_id":"5ac26645121f0613be08185a","name":"New York"},{"_id":"5ac26645121f0613be08185d","name":"Los Angeles"},{"_id":"5ac26648121f0613be081862","name":"Charlotte"},{"_id":"5ac2664a121f0613be081869","name":"Chicago"},{"_id":"5ac2664a121f0613be08186e","name":"Seattle"}]
When I console out just the first branches._id value, like so:
console.log("doc branch: ", docs[0].branches[0]._id);
I get:
doc branch: 5ac26645121f0613be08185a
And again, this check on the whether the value is a valid Mongo Object ID returns true:
console.log("is doc value valid: ", mongoose.Types.ObjectId.isValid(docs[0].branches[0]._id)); // returns true
So what's the problem here? Why am I getting this error:
Reason: TypeError: Cannot read property 'some' of undefined
When I do:
let filteredDocs = docs.filter(doc => doc.branches._id.some(branch => mongoArrBranchID.includes(branch._id)));
console.log("filteredDocs: ", filteredDocs);
And for extra clarification, when I use mock data in ScratchJS in Chrome, this works for me:
let docs = [
{
_id: "5ba39a12179b771820413ad8",
name: "Samson",
branches: [{ _id: "3nc26645121f0613be08167r", name: "New York" }, { _id: "3fc26645121f0613be08185d", name: "Los Angeles" }, { _id: "2hc26648121f0613be081862", name: "Seattle" }, { _id: "7jc2664a121f0613be081869", name: "Chicago" }, { _id: "7ju2664a121f0613be08186e", name: "Charlotte" }],
updatedAt: "2018-09-20T13:01:06.709Z",
createdAt: "2018-09-20T13:01:06.709Z"
},
{ _id: "3ya39a12179b771820413af5", name: "Sarah", branches: [{ _id: "3nc26645121f0613be08167r", name: "New York" }, { _id: "5ac26645121f0613be08145d", name: "Miami" }, { _id: "5ac2664a121f0613be08154s", name: "Sacramento" }], updatedAt: "2018-09-20T13:01:06.709Z", createdAt: "2018-09-20T13:01:06.709Z" },
{ _id: "2sa39a12179b771820413gy4", name: "Tim", branches: [{ _id: "1rd26645121d5613be08167h", name: "Denver" }, { _id: "5ac2664a121f0613be08154s", name: "Sacramento" }], updatedAt: "2018-09-20T13:01:06.709Z", createdAt: "2018-09-20T13:01:06.709Z" }
];
let filterValues = ["5ac26645121f0613be08145d", "7ju2664a121f0613be08186e"];
let filteredDocs = docs.filter(doc => doc.branches.some(branch => filterValues.includes(branch._id)));
console.log(filteredDocs);
So what's the difference? Why does it work in the mock example but not with my actual data?
It is because docs.branches is an array, and therefore does not have the _id attribute you have accessed on it. You should revise your code to the following:
let filteredDocs = docs.filter(doc => doc.branches.some(branch => mongoArrBranchID.includes(branch._id)));
The error you received occurred because accessing an non-existent attribute of an object returns undefined, so doc.branches._id returned undefined, and trying to access an attribute of undefined, some in this case, throws an error.
EDIT:
I want to clarify that the mistake is you wrote doc.branches._id.some instead of doc.branches.some in your code. The issue is the _id part.
Can anyone direct me how to display values of array in separate columns with ng-repeat? Please note that fields are dynamically generated and I cannot hardcode name of fields like tran.id, or tran.firstname....
thanks
My html is:
<tr ng-repeat="tran in trans">
<td>{{ tran }}</td>
</tr>
My JS code is:
$scope.displayTrans = function(){
$http.get("model/selecttrans.php")
.then(function(response) {
console.log(response.data);
$scope.trans = response.data;
});
}
and my PHP code is:
<?php
$sql = "SELECT * FROM trans";
$stmt = $conn->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
if ($total > 0 ) {
while ($row = $stmt->fetchObject()) {
$output[] = $row;
}
} else {
$output = 'No data';
}
echo json_encode($output);
I am getting following output in my console:
[…]
0: {…}
"$$hashKey": "object:15"
email: null
firstname: "Aziz"
id: "19"
lastname: "Virani"
password: "12345"
__proto__: Object { … }
1: {…}
"$$hashKey": "object:16"
email: "test#test.edu"
firstname: "Test"
id: "32"
lastname: "Result"
password: "test"
__proto__: Object { … }
length: 2
__proto__: Array []
and following output in my browser:
{"id":"19","lastname":"Virani","password":"12345","firstname":"Aziz","email":null}
{"id":"32","lastname":"Result","password":"test","firstname":"Test","email":"test#test.edu"}
Can any one suggest me how can I display output in separate columns as mentioned below:
id | lastname | password
32 | Result | test
Please ignore validation here like password should be hashed or md5 etc.....
I can easily get data by typing {{ tran.id }} or {{ tran.firstname }} but these fields are dynamically generated and i cannot hardcode fields name....
thank you
Aziz
So I figured out the way to display output....
Earlier I used nested ng-repeat with (key, value) with table row property but here on some other blog i found that nested ng-repeat works with table data so I updated my code and everything works cool....
I have looked at simular threads, but to no success. What I'm trying to do is update my localstorage through an update function. The functions look as follows:
The code to make the variables to call:
var localProfile = JSON.parse(localStorage.getItem("profile"));
if(localProfile != undefined && localProfile.length>0)
{ this.profiles = localProfile; }
else {
this.profile = [
{ id: "1526", name: "berserk", password: "berserk", age: "31", gender: "male"},
{ id: "1358", name: "Johnathan", password: "test", age: "17", gender: "male"},
{ id: "2539", name: "Britney", password: "test", age: "18", gender: "female"},
{ id: "1486", name: "Kevin", password: "test", age: "7", gender: "male"},
{ id: "7777", name: "jesus", password: "holy", age: "unknown", gender: "male"},
{ id: "6666", name: "satan", password: "hell", age: "unknown", gender: "unknown"}
];
}
The code to update the variable:
this.updateProfile = function(profile) {
profile.updating = false;
console.log(profile);
localStorage.setItem("profile", profile);
}
As I noted in the title I am currently using Angular. I have used the console.log(-line and the response seems to be exactly what it's supposed to be. I have tried using JSON.parse( and JSON.stringify as well as a couple of other combinations. I seem to get either the error above or another error when trying to reload the page. Apperently I either cannot execute the statement, or I end up corrupting the data so reloading the page returns a simular error.
In case the data in variable profile is in doubt:
Array [ Object, Object, Object, Object, Object, Object ]
And when taking a closer look at the data:
age:"17"
gender:"male"
id:"1358"
name:"johnathan"
password:"test"
The other object looks identical with no weird defaults in them. I already took care of the $$hashkey just incase that was the problem.
Any help on how to execute the call correctly is greatly apreciated and if the information is insufficient please do tell.
The problem is your not using JSON.stringify when saving your data. So when you are parsing from localStorage its not json.
Add a factory to be used across your application that handles JSON.parse() and JSON.stringify()
.factory('LocalStorageUtil', function() {
return {
get: get,
set: set,
remove: remove
}
function get(key) {
return JSON.parse(localStorage.getItem(key));
}
function set(key, val) {
localStorage.setItem(key, JSON.stringify(val));
}
function remove(key) {
return localStorage.removeItem(key);
}
})
Here is a JS Bin tiny sample app using this LocalStorageUtil.
http://jsbin.com/fijagiy/2/edit?js,output