如何使用Firebase在1个文档中插入数组

时间:2019-01-31 08:49:38

标签: angular firebase

我无法将数组存储到Firebase中的第一行中,但是成功插入了其他字段。

下面是我想要的文档结构:

transactions
  |
  |_ xxxxxx (id)
      | 
      |_in_account
      |_amount
      |....
      |_document
          |
          |_ xxxxx (id)
               |
               |_ file_name
               |_ file_url

transaction.component.ts

// Get value from form
let transaction = this.transactionForm.value;
let files = this.selectedFiles;

let document = {file: []} as any;
transaction.document = document;

let filesIndex = _.range(files.length);

_.each(filesIndex, (idx) => {
  this.currentUpload = new Upload(files[idx]);
  this.ts.pushUpload(transaction, this.currentUpload);
});

console.log(transaction); 

transaction.services.ts

pushUpload(transaction, upload: Upload) {
    let storageRef = this.fb.storage().ref();
    let uploadTask = storageRef.child(`${this.dbPath}/${upload.file.name}`).put(upload.file);

    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) =>  {
        // upload in progress
        upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100
      },
      (error) => {
        console.log(error);
        return error;
      },
      () => {
        upload.url = uploadTask.snapshot.downloadURL
        upload.name = upload.file.name
            // Here set the transaction document file array 
        transaction.document.file.push(upload); 
      }
    );
} 

当我在浏览器控制台中打印时,我能够看到文件的值,但是,当我检查Firebase控制台时,仅成功插入了金额,in_account和其他字段,但没有插入文档文件。

enter image description here

enter image description here

0 个答案:

没有答案