【发布时间】:2020-04-04 13:07:10
【问题描述】:
这是我为向集合添加数据而实现的代码:
import FirebaseKeys from "./config";
import firebase from "firebase";
import "@firebase/firestore";
class Fire {
constructor() {
firebase.initializeApp(FirebaseKeys);
}
addPost = async ({ text, localUri }) => {
const remoteUri = await this.uploadPhotoAsync(localUri);
const desc = text;
return new Promise((res, rej) => {
// console.log("THIS FIRESTORE" + this.firestore);
const dbh = firebase.firestore();
// this.firestore.collection("posts").add({
// text: desc,
// uid: this.uid,
// timestamp: this.timestamp,
// image: remoteUri
// })
dbh
.collection("posts")
.doc("feed")
.set({
text: desc,
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri
})
.then(ref => {
res(ref);
console.log("EVERYTHING IS FINE HERE");
})
.catch(error => {
console.log("ERROR HERE TOO");
rej(error);
});
});
};
uploadPhotoAsync = async uri => {
console.log(this);
const path = "Date.jpg";
return new Promise(async (res, rej) => {
const response = await fetch(uri);
const file = await response.blob();
let upload = firebase
.storage()
.ref(path)
.put(file);
upload.on(
"state_changed",
snapshot => {},
err => {
console.log("ERROR IN PHOTO UPLOAD");
rej(err);
},
async () => {
const url = await upload.snapshot.ref.getDownloadURL();
res(url);
console.log("IMAGE IS UPLOADING FINE");
}
);
});
};
get firestore() {
return firebase.firestore();
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
get timestamp() {
return Date.now();
}
}
Fire.shared = new Fire();
export default Fire;
我正在使用 expo react native 构建一个应用程序,并使用 firebase 进行数据操作。 但是在使用向集合添加数据的功能时,它会显示如下错误
undefined 不是对象(评估 'Wu.getRandomValues')
帮帮我
【问题讨论】:
标签: firebase react-native google-cloud-firestore expo