如何使用 react-native 在 Firestore 中的嵌套数组上添加数据
How to add data on nested array in Firestore with react-native
我想问一下是否可以在嵌套的array.The结果中添加数据我想要的是这个
但是当我使用我使用的代码添加新评级时我得到了这个
async function getAll(){
const userEmail= firebase.firestore().collection('users')
const snapshot=await userEmail.where('email','==',index.email).get()
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(doc => {
userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({parking:[markerName]})},{merge:true})
userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({rating:[currentRating]})},{merge:true})
console.log(userEmail.doc(doc.id));
});
}
getAll()
不幸的是,在 Firebase Firestore
中,您甚至无法更改特定 index
处的简单数组值。这也意味着您无法保存嵌套数组值。您可以在 .
上阅读更多相关信息
唯一的方法是下载整个数组进行修改,然后将整个数组再次保存到数据库中。您的应用程序上下文不多,因此我无法判断这是否适合您。这取决于数组中有多少数据。
我已经设法通过对图像路径数组使用 forEach 函数来做到这一点
const [imageUri, setImageUri] = useState([]);
const [uploading, setUploading] = useState(false);
const UploadImage = () => {
setUploading(true);
imageUri.forEach(async (image, index) => {
// setTransferred(0);
const pathToFile = image;
let filename = pathToFile.substring(pathToFile.lastIndexOf('-') + 1);
const extension = filename.split('.').pop();
const name = filename.split('.').slice(0, -1).join('.');
filename = name + Date.now() + '.' + extension;
const reference = storage().ref().child(`/userprofile/${filename}`);
// path to existing file on filesystem
// uploads file
const task = reference.putFile(pathToFile);
try {
await task;
const url = await reference.getDownloadURL();
downloadableURI.push(url);
if (index == imageUri.length - 1) {
setUploading(false);
Alert.alert(
'Image uploaded!',
'Your image has been uploaded to the Firebase Cloud Storage Successfully!',
);
}
} catch (e) {
console.log(e);
}
});
};
每当调用函数时,图像数组就会上传到 firebase 存储,
我想问一下是否可以在嵌套的array.The结果中添加数据我想要的是这个
但是当我使用我使用的代码添加新评级时我得到了这个
async function getAll(){
const userEmail= firebase.firestore().collection('users')
const snapshot=await userEmail.where('email','==',index.email).get()
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(doc => {
userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({parking:[markerName]})},{merge:true})
userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({rating:[currentRating]})},{merge:true})
console.log(userEmail.doc(doc.id));
});
}
getAll()
不幸的是,在 Firebase Firestore
中,您甚至无法更改特定 index
处的简单数组值。这也意味着您无法保存嵌套数组值。您可以在
唯一的方法是下载整个数组进行修改,然后将整个数组再次保存到数据库中。您的应用程序上下文不多,因此我无法判断这是否适合您。这取决于数组中有多少数据。
我已经设法通过对图像路径数组使用 forEach 函数来做到这一点
const [imageUri, setImageUri] = useState([]);
const [uploading, setUploading] = useState(false);
const UploadImage = () => {
setUploading(true);
imageUri.forEach(async (image, index) => {
// setTransferred(0);
const pathToFile = image;
let filename = pathToFile.substring(pathToFile.lastIndexOf('-') + 1);
const extension = filename.split('.').pop();
const name = filename.split('.').slice(0, -1).join('.');
filename = name + Date.now() + '.' + extension;
const reference = storage().ref().child(`/userprofile/${filename}`);
// path to existing file on filesystem
// uploads file
const task = reference.putFile(pathToFile);
try {
await task;
const url = await reference.getDownloadURL();
downloadableURI.push(url);
if (index == imageUri.length - 1) {
setUploading(false);
Alert.alert(
'Image uploaded!',
'Your image has been uploaded to the Firebase Cloud Storage Successfully!',
);
}
} catch (e) {
console.log(e);
}
});
};
每当调用函数时,图像数组就会上传到 firebase 存储,