JavaScript Mongoose DB脚本在drop()操作上出现停滞。

huangapple go评论59阅读模式
英文:

JavaScript Mongoose DB script hangs on drop() operation

问题

以下是翻译好的内容:

期望的行为:如果存在该集合,则删除它,否则继续执行脚本。

我的其中一个脚本永远无法通过drop()操作。

连接字符串是正确的,并且连接是活动的,因为其他脚本能够毫无问题地进行数据库插入。此外,console.log(connection.readyState);在挂起操作之前显示状态为1

我曾尝试过drop.database()drop()两种方式。

const axios = require("axios");
const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");
const mongoose = require("mongoose");
const connection = require("../../../config/connection");
const MdProdCat = require("../../../models/MdProdCat");

(async () => {
  try {

    // 下载 .xlsx 文件
    console.log("=== 开始 MD 产品类别导入...");
    console.log("下载 .xlsx 文件...");
    const response = await axios.get(
      "https://assets.mydeal.com.au/content/marketplace/MyDeal_Product_Category_List.xlsx",
      { responseType: "arraybuffer" }
    );
    console.log("保存 .xlsx 文件...");

    // 将文件保存到项目目录中
    console.log("保存 .xlsx 文件...");
    const xlsxFilePath = path.join(
      __dirname,
      "MyDeal_Product_Category_List.xlsx"
    );
    fs.writeFileSync(xlsxFilePath, Buffer.from(response.data));
    console.log("已保存 .xlsx 文件");

    // 将 .xlsx 文件转换为 .csv 格式
    console.log("将 .xlsx 转换为 .csv...");
    const workbook = XLSX.read(response.data, { type: "buffer" });
    const sheetNameList = workbook.SheetNames;
    const csvData = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetNameList[0]]);
    console.log("已转换 .xlsx 为 .csv");

    // 将 CSV 数据转换为 JSON 对象数组
    console.log("将 .csv 转换为 JSON...");
    const rows = csvData.split("\n").slice(1); // 跳过标题行
    const json = rows
      .filter((line) => line.trim()) // 跳过空行
      .map((line) => {
        const [CategoryID, Breadcrumbs] = line.split(",");
        return { _id: parseInt(CategoryID), Breadcrumbs };
      });
    console.log("已转换 .csv 为 JSON");

    // 检查与数据库的连接
    console.log("连接状态:", connection.readyState);
    // 等待连接建立
    connection.once('connected', async () => {

      // 尝试删除数据库如果已存在
      try {
        // 删除集合
        console.log("如果已存在则删除集合");
        await connection.dropCollection('mdprodcats');
        console.log("集合删除成功");
      } catch (error) {
        if (error.message === 'ns not found') {
          // 如果集合不存在则忽略错误
          console.log('集合不存在。继续...');
        } else {
          // 抛出其他错误
          throw error;
        }
      }

      // 将 JSON 数据插入到 MongoDB 集合中
      console.log("插入数据...");
      await MdProdCat.insertMany(json);
      console.log("数据插入成功");

      // 关闭连接
      console.log("关闭连接...");
      await connection.close();
      console.log("连接已关闭");
      console.log("=== 导入成功完成");

      process.exit();
    });

  } catch (error) {
    console.error("发生错误:", error);
    process.exit(1);
  }
})();

终端输出:

MongoDB 连接状态:2
=== 开始 MD 产品类别导入...
下载 .xlsx 文件...
保存 .xlsx 文件...
保存 .xlsx 文件...
已保存 .xlsx 文件
将 .xlsx 转换为 .csv...
已转换 .xlsx 为 .csv
将 .csv 转换为 JSON...
已转换 .csv 为 JSON
连接状态: 1

之后,脚本无限期地挂起。尝试过:删除drop()操作后,发现问题更广泛,因为随后的await MdProdCat.insertMany(json);也无法工作。

英文:

Desired behavior: drop the collection if it exists, otherwise proceed with script.

One of my scripts never gets past the drop() operation.

The connection string is correct, and the connection is active, because other scripts are able to make DB insertions without any issue. Furthermore, console.log(connection.readyState); gives a state of 1, right before the operation that hangs.

I have alternately tried drop.database() as well as drop().

const axios = require("axios");
const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");
const mongoose = require("mongoose");
const connection = require("../../../config/connection");
const MdProdCat = require("../../../models/MdProdCat");
(async () => {
try {
// Download the .xlsx file
console.log("=== Commencing MD Product Category import...");
console.log("Downloading .xlsx file...");
const response = await axios.get(
"https://assets.mydeal.com.au/content/marketplace/MyDeal_Product_Category_List.xlsx",
{ responseType: "arraybuffer" }
);
console.log("Saving .xlsx file...");
// Save the file to the project directory
console.log("Saving .xlsx file...");
const xlsxFilePath = path.join(
__dirname,
"MyDeal_Product_Category_List.xlsx"
);
fs.writeFileSync(xlsxFilePath, Buffer.from(response.data));
console.log("Saved .xlsx file");
// Convert the .xlsx file to .csv format
console.log("Converting .xlsx to .csv...");
const workbook = XLSX.read(response.data, { type: "buffer" });
const sheetNameList = workbook.SheetNames;
const csvData = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetNameList[0]]);
console.log("Converted .xlsx to .csv");
// Convert the CSV data to an array of JSON objects
console.log("Converting .csv to JSON...");
const rows = csvData.split("\n").slice(1); // skip the header row
const json = rows
.filter((line) => line.trim()) // skip empty rows
.map((line) => {
const [CategoryID, Breadcrumbs] = line.split(",");
return { _id: parseInt(CategoryID), Breadcrumbs };
});
console.log("Converted .csv to JSON");
// Check the connection to the database
console.log("Connection state: ", connection.readyState);
// Wait for the connection to be established
connection.once('connected', async () => {
// // Drop the database in case it already exists
// console.log("Dropping the database if it already exists");
// await connection.db.dropDatabase();
// Drop the collection in case it already exists
try {
// Drop the collection
console.log("Dropping collection if it already exists");
await connection.dropCollection('mdprodcats');
console.log("Collection dropped successfully");
} catch (error) {
if (error.message === 'ns not found') {
// Ignore the error if the collection does not exist
console.log('Collection does not exist. Continuing...');
} else {
// Throw any other errors
throw error;
}
}
// Insert the JSON data into the MongoDB collection
console.log("Inserting data...");
await MdProdCat.insertMany(json);
console.log("Data inserted successfully");
// Close the connection
console.log("Closing connection...");
await connection.close();
console.log("Connection closed");
console.log("=== Import completed successfully");
process.exit();
});
} catch (error) {
console.error("An error occurred:", error);
process.exit(1);
}
})();

Terminal output:

MongoDB connection state: 2
=== Commencing MD Product Category import...
Downloading .xlsx file...
Saving .xlsx file...
Saving .xlsx file...
Saved .xlsx file
Converting .xlsx to .csv...
Converted .xlsx to .csv
Converting .csv to JSON...
Converted .csv to JSON
Connection state:  1

After which the script hangs indefinitely.

Tried: removing the drop() operation reveals the problem to be of a broader nature as the subsequent await MdProdCat.insertMany(json); does not work either.

答案1

得分: 0

"Removing this wrapper worked:

connection.once('connected', async () => {

})

I still cannot understand why it worked, but it seems to have worked. Feel free to drop a comment below if you can explain why."

英文:

Removing this wrapper worked:

connection.once('connected', async () => {
})

I still cannot understand why it worked, but it seems to have worked. Feel free to drop a comment below if you can explain why.

huangapple
  • 本文由 发表于 2023年5月14日 03:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定