期望行为:如果集合存在,则删除它,否则继续执行脚本。
我的一个脚本从未通过drop()
操作。
连接字符串是正确的,并且连接是活动的,因为其他脚本能够毫无问题地进行DB插入。此外,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 {
// 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);
}
})();
端子输出:
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
之后,脚本无限期挂起。
已尝试:删除drop()操作揭示了更广泛的问题,因为后续的await MdProdCat.insertMany(json);
也不起作用。
1条答案
按热度按时间68de4m5k1#
删除此 Package 已成功:
我仍然不明白为什么它起作用了,但它似乎起作用了。如果你能解释原因,请在下面发表评论。