我已经使用JavaScript为Firebase创建了html表单。我有四个文本字段和一个时间戳。我有一个上传按钮,可以将文件(pdf)正确存储到Firebase存储器中。我想将Firebase存储中的文件的URL存储到Firestore数据库中。
我尝试了多种方法来获取URL并将其保存。我的问题似乎是“目的地:downloadUrl(signedUrl)”未存储文件的签名url,而是由于downloadUrl const导致了位置。我似乎无法弄清楚const上传实际网址所缺少的内容。我缺少语法上的某些内容,无法将手指放在上面。
const form = document.querySelector('#form_data');
const timestamp = firebase.firestore.FieldValue.serverTimestamp();
const downloadUrl = 'https://firebasestorage.googleapis.com/v0/b/company_info.appspot.com/Client_Uploads/o/';
//var app = firebase.initializeApp(config);
// Initialize Firebase
var config = {
apiKey: "Something",
authDomain: "Something",
databaseURL: "Something",
projectId: "Something",
storageBucket: "Something",
messagingSenderId: "Something"
};
firebase.initializeApp(config);
const db = firebase.firestore();
var storageRef = firebase.storage().ref();
// saving data credit_union_referrals
form.addEventListener('submit', (e) => {
e.preventDefault();
var fileButton = document.getElementById('fileButton');
var uploader = document.getElementById('uploader');
fileButton.addEventListener ('change', async function(e) {
var file = e.target.files[0];
var storageRef = firebase.storage().ref('Client_Uploads/'
+ file.name);
var task = storageRef.put(file);
const uploadOptions = {
destination: uploadFilePath,
public: true
};
let uploadedFile = await bucket.upload(tempThumbFilePath, uploadOptions)
const signedUrls = await uploadedFile[0].getSignedUrl({action: 'read', expires: '01-01-4499'})
const publicUrl = signedUrls[0];
})
db.collection("firestore_category").add({
lname: form.lname.value,
inlineFormCustomSelect: form.inlineFormCustomSelect.value,
additionalinformation: form.additionalinformation.value,
acceptance: form.acceptance.value,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
destination: downloadUrl(signedUr),
})
.then(function(docRef) {
console.log("Referral successfully sent!", docRef.id);
})
.catch(function(error){
console.error("Error sending referral: ", error);
});
form.something.value = '';
form.next.value = '';
form.additionalinformation.value = '';
form.acceptance.value = '';
})
预期结果是上载文件的url将保存到firestore数据库中。而是,文件会正确上传,但不会保存网址。任何帮助或指导,我们将不胜感激!