【发布时间】:2021-11-12 09:31:41
【问题描述】:
我不熟悉将 Cloudinary API 与 NuxtJS 一起使用。如何使用 NuxtJS 应用从 Cloudinary 帐户中删除图像/资产?
这是我尝试过的:
modules/cloudinary/index.js:(创建安全签名和配置信息)
import { createHash } from 'crypto'
import bodyParser from 'body-parser'
export default function () {
const config = this.options.privateRuntimeConfig.cloudinary
this.nuxt.hook('render:setupMiddleware', (app) => {
app.use(bodyParser.json())
app.use('/api/cloudinary/signature', setSignature)
})
function setSignature(req, res) {
try {
const sha1 = createHash('sha1')
const payload = []
Object.keys(req.body).forEach((key) => {
payload.push(`${key}=${req.body[key]}`)
})
sha1.update(payload.sort().join('&') + config.apiSecret)
res.end(
JSON.stringify({
signature: sha1.digest('hex')
})
)
} catch (error) {
console.error(error)
}
}
}
DeleteImage.vue:
<script>
import cloudinary from 'cloudinary' <--// Node SDK: https://cloudinary.com/documentation/node_integration
export default {
data() {
return {
loading: false,
src: null
}
},
methods: {
async deleteFile() {
const response = await fetch('/api/cloudinary/signature', {
method: 'POST',
// body: JSON.stringify(options),
headers: {
'Content-Type': 'application/json'
}
})
const signature = response.json.signature
console.log(signature) // <-- console logging here gets me the signature
try {
const asset = await cloudinary.v2.uploader.destroy(this.src, signature)
console.log(asset)
} catch (error) {
console.error('error deleting image', error)
}
},
}
}
</script>
我现在收到控制台错误:
cloudinary.js?9447:1 Uncaught TypeError: Cannot read properties of undefined (reading 'split')
at Object.eval (cloudinary.js?9447:1)
at eval (cloudinary.js:10)
at Object../node_modules/cloudinary/cloudinary.js (app.js:5178)
at __webpack_require__ (runtime.js:854)
at fn (runtime.js:151)
at eval (index.js?!./node_modules/vue-loader/lib/index.js?!./components/UploaderImage.vue?vue&type=script&lang=js&:30)
at Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./components/UploaderImage.vue?vue&type=script&lang=js& (app.js:1546)
at __webpack_require__ (runtime.js:854)
at fn (runtime.js:151)
at eval (UploaderImage.vue?9448:1)
我不知道我在做什么。对不起!
【问题讨论】:
-
嗨,到目前为止,您尝试了什么?您可以从 Cloudinary 的仪表板中删除它。 destroy 方法看起来适合您的用例:cloudinary.com/documentation/…
-
试图理解……我可以在常规 SFC 中使用该 API 还是需要在某种插件中进行设置?
-
用我的尝试更新了我的问题。我不知道我在做什么,对不起。我不能像那样将 Node SDK 导入 VUE 文件吗?
标签: nuxt.js cloudinary