但出现很多错误,例如:
UnhandledPromiseRejectionWarning:MongoError:给定事务编号5与任何正在进行的事务都不匹配。
我正在使用以下代码:
collection.js包含以下帮助器函数,用于将db命令包装在其他地方的事务中(所有这些都在服务器端):
const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
export const RunInMongoTx = async function(func) {
try {
const session = await client.startSession();
session.startTransaction();
options = { session };
func(options);
session.commitTransaction();
} catch (error) {
session.abortTransaction();
throw error;
}
};
然后在其他文件中,导入该函数并使用它:
import {RunInMongoTx} from "./collections";
Meteor.methods({
"ShiftRequests.setAsDidNotTurnUp": sr => {
const job = Jobs.findOne({ _id: sr.jobId });
if (job.poster != Meteor.userId()) {
throw new Meteor.Error(
"not-job-poster",
"Only poster can setAsDidNotTurnUp"
);
}
RunInMongoTx(async options => {
res = await EmployerFeedbacks.rawCollection().remove(
{
jobId: job._id,
workerId: sr.workerId
},
options
);
res = await ShiftRequests.rawCollection().update(
{ _id: sr._id },
{ $set: { didNotTurnUp: true } },
options
);
});
}
});
答案 0 :(得分:1)
我认为您的问题是您在不等待的情况下调用RunInMongoTx函数。
const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
export const RunInMongoTx = async function(func) {
try {
const session = await client.startSession();
session.startTransaction();
options = { session };
await func(options);
session.commitTransaction();
} catch (error) {
session.abortTransaction();
throw error;
}
};
import {RunInMongoTx} from "./collections";
Meteor.methods({
"ShiftRequests.setAsDidNotTurnUp": async sr => {
const job = Jobs.findOne({ _id: sr.jobId });
if (job.poster != Meteor.userId()) {
throw new Meteor.Error(
"not-job-poster",
"Only poster can setAsDidNotTurnUp"
);
}
await RunInMongoTx(async options => {
res = await EmployerFeedbacks.rawCollection().remove(
{
jobId: job._id,
workerId: sr.workerId
},
options
);
res = await ShiftRequests.rawCollection().update(
{ _id: sr._id },
{ $set: { didNotTurnUp: true } },
options
);
});
}
});
让我知道这是否可行。