我了解储蓄的概念和最佳实践结构化数据 https://www.firebase.com/docs/web/guide/structuring-data.html在 firebase 中,但我不清楚如何将数据实际保存到多个位置并提供所需的交叉引用。
{
articles: {
-KBX5TurV9uJTeiR26-N: {
title: 'post title 1',
body: 'post body goes here',
imagesRef: {
-KBX5XOYASP7h2ZPOKmg: true
}
},
-KCe7cy6QC29WRYap0D1: {
title: 'post title 2',
body: 'post body goes here',
imagesRef: {
-KBX5XOYASP7h2ZPOKmg: true
}
}
}
images: {
-KBX5XOYASP7h2ZPOKmg: {
image: 'data:image/gif;base64,R0lGODlhZABkAOYAAPj4...',
articlesRef: {
-KBX5TurV9uJTeiR26-N: true,
-KCe7cy6QC29WRYap0D1: true
}
}
}
}
上面的模式是我想要的一个例子。在提交由标题、正文和图像字段组成的表单时,我需要将标题和正文发布到articles对象,然后将图像发布到images对象,然后使用交叉引用更新imagesRef和articlesRef对象。在 Angular 中解决这个问题的最佳方法是什么?我也有 Angularfire 作为项目的一部分。
我是 Angular 和 Firebase 的新手,但我认为我需要写两篇文章,一篇文章,一篇图片,以保存它们并生成唯一的 id。然后,我需要知道如何等待两者都成功,获取唯一的 id,并对两个对象进行更新以保存交叉引用。这是我到目前为止所拥有的,但不知道如何进一步......
$scope.AddPost = function(){
var ref = new Firebase(FIREBASE_URI);
var newArticleRef = ref.child('articles').push();
var newArticleKey = newArticleRef.key();
// Create the data we want to update
var addNewPost = {};
addNewPost["articles/" + newArticleKey] = {
title: $scope.article.title,
post: $scope.article.post
};
if ($scope.image) {
var newImageRef = ref.child('images').push();
var newImageKey = newImageRef.key();
// Add image...
addNewPost['images/' + newImageKey] = {
image: $scope.image
};
//Add article ref...
addNewPost['images/' + newImageKey + '/articles/' + newArticleKey] = true;
//Add cross ref to article...
addNewPost['articles/' + newArticleKey + '/image/' + newImageKey] = true;
}
// Do a deep-path update
ref.update(addNewPost, function(error) {
if (error) {
console.log("Error:", error);
}
});
});
UPDATE:我更新了代码示例以利用多地点更新 https://www.firebase.com/blog/2015-09-24-atomic-writes-and-more.html和扇出方法 https://www.firebase.com/blog/2015-10-07-how-to-keep-your-data-consistent.html,但我在保存时遇到错误...
Error: Firebase.update failed: First argument contains a path /images/-KCpx-Pj9oMM-EWN9irS that is ancestor of another path /images/-KCpx-Pj9oMM-EWN9irS/articles/-KCpx-Pj9oMM-EWN9irR
at Error (native)
at ig (http://localhost:8000/app/assets/js/plugins.js:430:390)
at jg (http://localhost:8000/app/assets/js/plugins.js:431:383)
at X.update (http://localhost:8000/app/assets/js/plugins.js:564:369)
at r.$scope.AddPost (http://localhost:8000/app/assets/js/app.js:171:17)
at fn (eval at <anonymous> (http://localhost:8000/app/assets/js/plugins.js:216:110), <anonymous>:4:212)
at e (http://localhost:8000/app/assets/js/plugins.js:257:177)
at r.$eval (http://localhost:8000/app/assets/js/plugins.js:133:446)
at r.$apply (http://localhost:8000/app/assets/js/plugins.js:134:175)
at r.scopePrototype.$apply (chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:1427:22)
WARNING:我尝试了以下方法。它创建了一个对象,并且没有触发 JS 错误,但它清除了我的数据库,只留下新创建的对象。没有损失太多,因为我现在只是在运行一些测试,但我认为我应该警告其他人。不要这样做...
addNewPost.Articles = {};
addNewPost.Articles[newArticleKey] = {};
addNewPost.Articles[newArticleKey].title = $scope.article.title;
addNewPost.Articles[newArticleKey].post = $scope.article.post;
var newImageRef = ref.child('images').push();
var newImageKey = newImageRef.key();
addNewPost.images = {};
addNewPost.images[newImageKey] = {};
addNewPost.images[newImageKey].image = $scope.image;
addNewPost.images[newImageKey].articles = {};
addNewPost.images[newImageKey].articles[newArticleKey] = true;
addNewPost.Articles[newArticleKey].image = {};
addNewPost.Articles[newArticleKey].image[newImageKey] = true;