我在NodeJS中上传图片到cloudinary时遇到了问题,因为当我尝试时,我得到了这个错误
Error [ERR_SOCKET_CONNECTION_TIMEOUT]: Socket connection timeout
{
error: Error [ERR_SOCKET_CONNECTION_TIMEOUT]: Socket connection timeout
at new NodeError (node:internal/errors:399:5)
at internalConnectMultiple (node:net:1099:20)
at Timeout.internalConnectMultipleTimeout (node:net:1638:3)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
code: 'ERR_SOCKET_CONNECTION_TIMEOUT'
}
}
有时候图片会被上传,有时候不会。我在网上查了一下,上面说互联网连接很差,但我的互联网足够好,我已经把整个应用程序对接了,所以如果这与它有关的话,我就不知道了。
const addProduct = async (req: Request, res: Response, next: NextFunction) => {
const {
title,
snippet,
description,
quantity,
price,
coverImage,
imageArray,
category,
} = req.body;
try {
cloudinary.api
.ping()
.then((res) => {
console.log(`Cloudinary connection ${res.status}`);
})
.catch((err) => console.log(err));
const imageUrlArray: Array<imageObjectType> = [];
const coverImageUpload = await cloudinary.uploader.upload(coverImage);
if (imageArray !== undefined) {
for (let i = 0; i < imageArray.length; i++) {
const image = await cloudinary.uploader.upload(imageArray[i]);
imageUrlArray.push({
publicId: image.public_id,
secureUrl: image.secure_url,
});
}
}
console.log(req.seller);
const product = await Product.create({
title: title,
snippet: snippet,
description: description,
quantity: quantity,
price: price,
coverImage: {
publicId: coverImageUpload.public_id,
secureUrl: coverImageUpload.secure_url,
},
imageArray: imageUrlArray,
category: category,
sellerId: req.seller,
});
console.log(product);
if (product) {
res.status(200).json({
message: "Product added",
category: category,
});
}
} catch (err) {
console.log(err);
}
};
这是我上传图像的地方,它在我试图ping到cloudinary的位置给了我错误。
1条答案
按热度按时间mctunoxg1#
我也遇到过同样的问题,在我的情况下,这是一个错误与节点20. 0. 0版本。为了解决这个问题,我必须添加一个环境变量NODE_OPTIONS=no-network-family-autoselection