我正在尝试将 Node.js 服务器连接到 Atlas mongo 数据库。我为此使用猫鼬。
\nawait mongoose\n .connect(process.env.MONGO_URI!, {\n useNewUrlParser: true,\n useUnifiedTopology: true,\n useCreateIndex: true,\n useFindAndModify: false,\n poolSize: parseInt(process.env.POOL_SIZE!),\n })\n .then((res) => {\n console.log(\n 'Connected to Distribution API Database - Initial Connection'\n );\n })\n .catch((err) => {\n console.log(\n `Initial Distribution API Database connection error occured -`,\n err\n );\n });\nRun Code Online (Sandbox Code Playgroud)\n我在 package.json 文件中与此相关的依赖项如下
\n"dependencies": {\n "@types/mongoose": "5.7.29",\n "mongoose": "5.9.21",\n "typescript": "3.9.5"\n },\nRun Code Online (Sandbox Code Playgroud)\n这是早些时候工作的,没有任何问题(我根本没有更新 @types/mongoose 或 mongoose 版本),现在突然出现以下错误
\nCompilation error in /app/src/index.ts\nUsing ts-node version 8.10.2, typescript …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用事务来更新多个文档。
一种是加载表单文档 [await Sheet.save({ session });]
另一个是库存预订记录数组 [await Stock.bulkWrite()]。
const session = await mongoose.startSession();
session.startTransaction({
readPreference: 'primary',
readConcern: { level: 'local' },
writeConcern: { w: 'majority' },
});
let sheetAfterSave: any = null;
try {
sheetAfterSave = await sheet.save({ session });
records.forEach(async (el: any) => {
let updatedStockRecord = await Stock.bulkWrite(
[
{
updateOne: {
filter: {
index: el.index,
product: el.product,
batchNo: el.batchNo,
agency,
totalQuantity: { $gte: el.loadingTotal },
},
update: {
$push: {
reservations: {
loadingSheetId: sheetAfterSave._id,
reservedCaseQuantity: el.loadingCaseCount, …Run Code Online (Sandbox Code Playgroud)