尝试将图像上传到 Cloudinary 时收到 TypeError
Receive TypeError when trying to upload an image to Cloudinary
我一直在尝试将图片直接从我的表单上传到 cloudinary。
每当我尝试上传图片时,都会收到以下错误消息:
/mnt/c/NodeApps/uploadFile/node_modules/multer-storage-cloudinary/lib/index.js:67
var stream = _this.cloudinary.v2.uploader.upload_stream(params, cb);
^
TypeError: Cannot read property 'uploader' of undefined
at /mnt/c/NodeApps/uploadFile/node_modules/multer-storage-cloudinary/lib/index.js:67:42
at end (/mnt/c/NodeApps/uploadFile/node_modules/run-parallel/index.js:18:15)
at process._tickCallback (internal/process/next_tick.js:61:11)
我的表格:
<form action='upload' method="POST" enctype="multipart/form-data">
<input type='text' name='caption' placeholder="Name the image">
<input type="file" name="new_image">
<button type='submit'>Submit</button>
</form>
来自app.js
var multer = require('multer');
var cloudinary = require('cloudinary').v2;
var cloudinaryStorage = require('multer-storage-cloudinary');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:true}));
Cloudinary 配置:
cloudinary.config({
cloud_name: process.env.cloud_name,
api_key: process.env.API_KEY,
api_secret: process.env.API_SECRET
});
var storage = cloudinaryStorage({
cloudinary: cloudinary,
folder: "demo",
allowedFormats: ['jpg', 'png'],
});
var parser = multer({ storage: storage });
我的Post路线:
app.post('/upload', parser.single('new_image'), (req, res) => {
cloudinary.v2.uploader.upload(req.file.path, (err, result) => {
if (err) { console.log('Error: ' + err); }
image.url = result.secure_url;
console.log(image.url);
});
});
我完全无法解决这个错误,非常感谢任何关于我哪里出错的建议。
我认为你忽略了一个小错误——当你在这里需要它时,你已经遍历到了 SDK 的 v2 对象:
var cloudinary = require('cloudinary').v2;
因此,在这段代码中:
cloudinary.v2.uploader.upload(req.file.path, (err, result) => {
...
}
您实际上是在调用 cloudinary.v2.v2.uploader
而不是 cloudinary.v2.uploader
。因此,由于 cloudinary.v2.v2
未定义,因此无法在未定义的情况下调用上传器方法的错误。在您的要求行或从您调用上传者的地方删除 .v2
。
另一个问题是,根据您的代码,您似乎将成为 double-uploading 图片。由于您已将 multer 配置为使用 Cloudinary 作为存储目的地,将其作为中间件应该意味着它会自行上传到 Cloudinary,因此当您调用 .uploader.upload()
时,这实际上是第二次上传。我相信这会是更正确的代码:
app.post('/upload', parser.single('new_image'), (req, res) => {
// Since multer is the middleware & set to single, req.file should be an object with the results from cloudinary
let image = {};
image.url = req.file.secure_url;
console.log(image.url);
});
我发现一些有用的参考资料:
我一直在尝试将图片直接从我的表单上传到 cloudinary。 每当我尝试上传图片时,都会收到以下错误消息:
/mnt/c/NodeApps/uploadFile/node_modules/multer-storage-cloudinary/lib/index.js:67
var stream = _this.cloudinary.v2.uploader.upload_stream(params, cb);
^
TypeError: Cannot read property 'uploader' of undefined
at /mnt/c/NodeApps/uploadFile/node_modules/multer-storage-cloudinary/lib/index.js:67:42
at end (/mnt/c/NodeApps/uploadFile/node_modules/run-parallel/index.js:18:15)
at process._tickCallback (internal/process/next_tick.js:61:11)
我的表格:
<form action='upload' method="POST" enctype="multipart/form-data">
<input type='text' name='caption' placeholder="Name the image">
<input type="file" name="new_image">
<button type='submit'>Submit</button>
</form>
来自app.js
var multer = require('multer');
var cloudinary = require('cloudinary').v2;
var cloudinaryStorage = require('multer-storage-cloudinary');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:true}));
Cloudinary 配置:
cloudinary.config({
cloud_name: process.env.cloud_name,
api_key: process.env.API_KEY,
api_secret: process.env.API_SECRET
});
var storage = cloudinaryStorage({
cloudinary: cloudinary,
folder: "demo",
allowedFormats: ['jpg', 'png'],
});
var parser = multer({ storage: storage });
我的Post路线:
app.post('/upload', parser.single('new_image'), (req, res) => {
cloudinary.v2.uploader.upload(req.file.path, (err, result) => {
if (err) { console.log('Error: ' + err); }
image.url = result.secure_url;
console.log(image.url);
});
});
我完全无法解决这个错误,非常感谢任何关于我哪里出错的建议。
我认为你忽略了一个小错误——当你在这里需要它时,你已经遍历到了 SDK 的 v2 对象:
var cloudinary = require('cloudinary').v2;
因此,在这段代码中:
cloudinary.v2.uploader.upload(req.file.path, (err, result) => {
...
}
您实际上是在调用 cloudinary.v2.v2.uploader
而不是 cloudinary.v2.uploader
。因此,由于 cloudinary.v2.v2
未定义,因此无法在未定义的情况下调用上传器方法的错误。在您的要求行或从您调用上传者的地方删除 .v2
。
另一个问题是,根据您的代码,您似乎将成为 double-uploading 图片。由于您已将 multer 配置为使用 Cloudinary 作为存储目的地,将其作为中间件应该意味着它会自行上传到 Cloudinary,因此当您调用 .uploader.upload()
时,这实际上是第二次上传。我相信这会是更正确的代码:
app.post('/upload', parser.single('new_image'), (req, res) => {
// Since multer is the middleware & set to single, req.file should be an object with the results from cloudinary
let image = {};
image.url = req.file.secure_url;
console.log(image.url);
});
我发现一些有用的参考资料: