javascript - 未捕获的 ReferenceError:未定义常量
问题描述
在我看来,我有这行 html:<div id="ajax" data-value="photos_path"></div>
然后在我的js中我这样做:
import 'uppy/dist/uppy.min.css'
import '@uppy/core/dist/style.min.css'
import '@uppy/webcam/dist/style.min.css'
import {
Core,
Dashboard,
Webcam,
AwsS3,
} from 'uppy'
function fileUpload(fileInput) {
const hiddenInput = document.querySelector('.upload-data'),
const post_url = document.getElementById('ajax').getAttribute('data-value'),
const s3_url = document.getElementById('s3').getAttribute('data-value')
const uppy = Core({
autoProceed: true,
allowMultipleUploads: true,
restrictions: {
maxFileSize: 10000000,
maxNumberOfFiles: 10,
allowedFileTypes: ['image/jpeg', 'image/png']
}
})
.use(Dashboard, {
inline: true,
target: '#file-uppy',
trigger: '.upload-file',
hideUploadButton: true,
replaceTargetContent: false,
showProgressDetails: true,
width: 1200,
height: 400,
})
.use(Webcam, {
target: Dashboard,
})
.use(AwsS3, {
companionUrl: s3_url,
})
uppy.on('upload-success', (file, response) => {
const uploadedFileData = {
id: file.meta['key'].match(/^cache\/(.+)/)[1], // object key without prefix
storage: 'cache',
metadata: {
size: file.size,
filename: file.name,
mime_type: file.type,
}
}
$.ajax({
type: "POST",
url: post_url,
data: {
photo: {
image: uploadedFileData
}
}
})
})
}
export default fileUpload
然而由于某种原因,js没有编译。我得到这个错误:Uncaught ReferenceError: post_url is not defined
。在我看来它是定义的?事实上,我对另一个数据值做同样的事情。<div id="s3" data-value="docs"></div>
在视图中和s3 = document.getElementById('s3').getAttribute('data-value')
。这非常完美。
为什么一个有效而另一个无效?
解决方案
改变这个:
post_url = document.getElementById('ajax').getAttribute('data-value')
对此:
const post_url = document.getElementById('ajax').getAttribute('data-value')
与 javascript 中的 python 和 ruby 不同,您需要使用关键字(const、let 或 var)来声明变量或常量。
推荐阅读
- bazel - 如何增加重试次数或延长抓取时间?
- java - 打印字符串数组中的所有名称
- apache - 在 Apache 上的 LDAP 身份验证后传递用户名
- jquery - 使用 html ids 查询 mongoDB 文档
- r - 使用 dplyr 的 F 检验方差
- python - 这个运算符“>>”和“//”是什么意思?
- javascript - Print/log objects from an array using a for loop method
- sas - 需要关于如何合并具有相似但不相同关节 id 的数据集的建议
- caching - Google Apps 脚本 - 提交表单时传递的打开表单中的查询参数
- python - 为什么 for 循环会垂直产生输出?