New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-path update #435
Comments
It works the same way :) The constructor(af: AngularFire) {
var updatedUserData = {};
updatedUserData["user/posts/" + newPostKey] = true;
updatedUserData["posts/" + newPostKey] = {
title: "New Post",
content: "Here is my new post!"
};
af.database.object('path').update(updatedUserData);
} |
Thanks David. You save my time !! |
@davideast
So maybe that is the intention with multi-path? To me the name implies it should be an update rather than a set operation... eg. if I start off with firebase data of: constructor(af: AngularFire) {
let update = {};
update['test/objectA'] = {a:10, e:50};
update['test/objectB'] = {x:20};
af.database.object('').update(update);
} Then I end up with: rather than the expected (if it was an update) |
@mikeybyker @davideast I'm experiencing the same thing. Perhaps Firebase 3 doesn't support this? When I run this and actually allow the write in my database rules // Create an object representing our complete changes
let databaseUpdate = {
// Add the company change to the update
companies: company, // company object is defined elsewhere
users: {}
};
// Build the user object
let userCompanies = {};
userCompanies[companyKey] = employeeKey;
let user = {
companies: userCompanies
};
// Add the user object to the database tree at the user id's location
databaseUpdate.users[this.auth.auth.uid] = user;
return this.af.database.object('').update(databaseUpdate); It just replaces the whole database |
Works for me. |
I thought I'd tried everything - thanks @expleosoftware! I don't know how I missed that. |
@robodair for me, using update still doesn't work as I had in my example: constructor(af: AngularFire) {
let update = {};
update['test/objectA'] = {a:10, e:50};
update['test/objectB'] = {x:20};
af.database.object('').update(update);
} That does a set operation - or at least, overwrites the entire object. Any values in the fb database (eg. objectA.b, objectA.c, objectA.d) not specified in the update object are deleted. Which is a shame - lost a lot of data finding that out! However, a way round it, and maybe how it is meant to work here, is to go further down the path, so to speak: constructor(af: AngularFire) {
let update = {};
update['test/objectA/a'] = 10;
update['test/objectA/e'] = 50;
update['test/objectB/x'] = 20;
af.database.object('').update(update);
} So update['test/objectA/a'] = 10 rather than update['test/objectA'] = {a:10}; |
...although if you look at how firebase themselves say it should work it doesn't appear to be working the same way...
{
"user": {
"name": "Samantha",
"age": 25
}
} var userRef = ref.child("user");
var newUserData = {
"age": 30,
"city": "Provo, UT"
};
ref.child("user").update(newUserData); So, still not sure if an angularfire bug or not...Maybe @davideast could say? |
try constructor(af: AngularFire) {
let update = {};
update['test/objectA'] = {a:10, e:50};
update['test/objectB'] = {x:20};
af.database.object('/').update(update);
} the af.database.object('/').update(update); is what made it work for me |
@robodair Normal (single) updates work fine - i.e. this.af.database.object('test/objectA').update({ a: 99 }); Updates 'a' and leaves b,c,d,e alone. Have a look: https://mikeybyker.github.io/multi-location-updates/ |
@mikeybyker you're right. This is hella confusing. It just so happens that for my application there's nothing that exists at said locations so I didn't notice. Thanks for the test repo, I made a fork and submitted a pull request with explanations. Basically |
No problem :-) Yep, a bit confusing is right! |
@wceolin - it does perform a set() yup. To me it would be a bug, and it doesn't fit with what firebase have said about how it should work (see quote & link above) - but the description used by @davideast at the top of this issue ("It really just calls set anyways.") sounds like they don't think it is... Which doesn't help much - sorry! |
It performs an update at the first level, but everything below is treated as a set. To get the effects of an update you'll have to go to the deepest level of the path. Rather than: let update = {};
update['test/objectA'] = {a:10, e:50};
update['test/objectB'] = {x:20}; Do this:
By specifying deep path level it will only do an update for |
My guess is that it would be too expensive of an operation on the firebase
|
how do you do this in angularfire2 4.0? |
I hope my solution could be useful for someone:
It works fine for me. |
Here is a video about this feature on the Firebase youtube channel: This post gives a little more explanations: |
If I give a type to document reference, then running update on it with multi-path gives type error. Any idea how to solve that? export interface Personal{
tel: number
}
export interface LocalUser{
personal: Personal
}
public userRef = (id: string): AngularFirestoreDocument<LocalUser> => this.afs.doc(`users/${id}`);
this.usrRef.update({"personal/tel": 237486238746}) this gives |
Hi
Is there a way to perform multi-path update (like described here: https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html) using angularfire2 ?
The text was updated successfully, but these errors were encountered: