Node js中的Socket连接超时错误

问题描述 投票:0回答:0

我一直无法在 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'
   }
 }

有时会上传图片,有时不会。我在网上查了一下,它说互联网连接很差,但我的互联网已经足够好了,我已经将整个应用程序 dockerized,所以如果这与它有关,我就知道了。

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 的地方给了我错误。

javascript node.js sockets cloudinary
© www.soinside.com 2019 - 2024. All rights reserved.