我的JavaScript显示错误:
firebase.storage()不起作用
但这在Google代码实验室的示例代码中起作用。
let file;
function handleFileUploadChange(e) {
file = e.target.files[0];
}
function handleFileUploadSubmit(e) {
console.log("handgle");
firebase.firestore().collection('messages').add({
name: "sanket",
imageUrl: " ",
}).then(function (messageRef) {
// 2 - Upload the image to Cloud Storage.
var filePath = firebase.auth().currentUser.uid + '/' + messageRef.id
+ '/' + file.name;
return firebase.storage().ref(filePath).put(file).then(function
(fileSnapshot) {
// 3 - Generate a public URL for the file.
return fileSnapshot.ref.getDownloadURL().then((url) => {
// 4 - Update the chat message placeholder with the image's
return messageRef.update({
imageUrl: url,
storageUri: fileSnapshot.metadata.fullPath
});
});
});
}).catch(function (error) {
console.error('There was an error uploading a file to Cloud
Storage:', error);
});
}
var change = document. getElementById('file-select');
change. addEventListener('change', handleFileUploadChange);
var sumbit = document. getElementById('file-submit');
sumbit. addEventListener('click', handleFileUploadSubmit);
<div id="filesubmit">
<input type="file" id="file-select" accept="image/*"/>
<input type="button" id="file-submit" value="SUBMIT">
</div>