首先,我不知道我是否在这里使用了正确的术语,所以事先做了很大的道歉。我会尽力解释我想要做的事情。
我有什么
我有一个返回以下内容的对象:
var upload = {
created: "21/07/2017",
name: "album photo name",
progress:100,
url:"www.url.com/photo.jpg"
}
我正在尝试使用其中一个对象中的URL值来执行数据库更新
var myList = {
albumtitle: title,
albumDate: date,
album_photo_url: upload.url
}
执行数据库更新时,控制台会记录:
错误:未捕获(在承诺中):错误:Firebase.update失败:第一个参数容器在属性'album.jdslakjdlkas.sjdlkajsd.album_photo_url'中未定义
问题 我使用的语法是否正确?当我删除album_photo_url行时,其他数据正确上传。或者如果我写:
album_photo_url:upload
然后,所有内容都会使用嵌套节点上传到数据库,该节点包含上传进度,名称,日期和URL。如何访问对象引用中的URL?
这里的任何帮助都会受到广泛赞赏。
修改 这是我的完整代码段:
import { Injectable } from '@angular/core';
// import { AngularFire, AngularFireDatabase, FirebaseListObservable } from 'angularfire2';
import { Upload } from './upload';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import * as firebase from 'firebase';
import { AuthService } from '../../auth.service';
@Injectable()
export class UploadService {
constructor(private db: AngularFireDatabase, private authService: AuthService) { }
private basePath: string = '/site_photo';
uploads: FirebaseListObservable<Upload[]>;
getUploads(query = {}) {
this.uploads = this.db.list(this.basePath, {
query: query
});
return this.uploads
}
submitForm(title: string, date: string, upload: Upload) {
this.authService.user.subscribe(user => {
if (user) {
console.log(upload.url);
// The User ID of the current user
var userId = user.uid;
// Data to be added to the 'Album' node
var projectsDetails = {
albumTitle: title,
albumDate: reference,
album_photo_url: upload.url
}
// Data to be added to the 'Album List' node
var projectsList = {
albumTitle: title,
albumDate: reference,
album_photo_url: upload.url
}
// Generate a new firebase key to be used for the Project, Project List and Project Member nodes
var newPostKey = firebase.database().ref().child('album_list').push().key;
// Create a reference to the firebase storage
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child('site_photo/' + user.uid + '/' + newPostKey + '/site_photo').put(upload.file);
// Group the updates to the associated firebase nodes
var updates = {};
updates['/album/' + user.uid + '/' + newPostKey] = projectsDetails;
updates['/album_list/' + user.uid + '/' + newPostKey] = projectsList;
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
// upload in progress
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error)
},
() => {
// upload success
upload.url = uploadTask.snapshot.downloadURL
upload.name = upload.file.name
this.saveToRealtimeDatabase(updates);
}
);
}
});
}
private saveToRealtimeDatabase(updates) {
// Perform an atomic Multi-Path update
return firebase.database().ref().update(updates);
}
}