如何将图像网址数组上传到Firestore?

时间:2019-08-23 21:19:31

标签: reactjs firebase react-native google-cloud-firestore

您好,如何在React Native中将图像网址数组上传到Firestore 我尝试过

query {
  json(id: 1) {
    data {
      #whatever data is has?
    }
  }
}

--would return--

{
  "data": {
    "foo": "bar"
  }
}

但是图像会在Firestore数据库中显示为这样

 state = { photos: [] };

buttonPress() {

const { photos } = this.state; 

firebase.firestore().collection('users').doc('images')
.add({ photos });
}

我希望它显示在Firestore数据库中的这样的数组中

{
"book": "Robin hood", 
"photos": "https://picsum.photos/id/1001/5616/3744",
 }

1 个答案:

答案 0 :(得分:2)

这可以通过将photos包装在[]中来实现。因此,您的代码将是;

firebase.firestore().collection('users').doc('images')
  .add({ [photos] });
}

此外,如果您希望用户以后添加而不覆盖数组,我建议将setmerge: true一起使用。

firebase.firestore().collection('users').doc('images')
  .set({ [photos] }, { merge: true });
}