Sum*_*mer 6 javascript rest file-upload node.js typescript
我在 Node JS 中将图像上传到 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'
}
}
Run Code Online (Sandbox Code Playgroud)
有时图像会上传,有时则不会。我在网上查了一下,它说互联网连接很差,但我的互联网足够好,我已经将整个应用程序进行了 Docker 化,所以我不知道这是否与它有关。
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);
}
};
Run Code Online (Sandbox Code Playgroud)
这是我上传图像的地方,在我尝试 ping 到 cloudinary 时出现错误。
小智 2
我遇到了同样的问题,就我而言,这是节点 20.0.0 版本的错误。为了解决这个问题,我必须添加一个环境变量 NODE_OPTIONS=no-network-family-autoselection