我当前遇到以下问题,但我将首先展示我的模式:
会员:
const {Schema, model} = require('mongoose')
const Mongoose = require('mongoose')
const Member = Schema ({
name: String,
discordId:{
type: String,
required: true,
unique: true
},
rank: String,
rslevel: Number,
wsStatus: String,
Corp: {type: Mongoose.Types.ObjectId, ref: "Corp"},
timezone: String,
SupportShip: String,
techs: [{ type: Mongoose.Types.ObjectId, ref: 'Tech' }],
battleship: { type: Mongoose.Types.ObjectId, ref: 'Battleship' },
transport: { type: Mongoose.Types.ObjectId, ref: 'Transport' },
miner: { type: Mongoose.Types.ObjectId, ref: 'Miner' }
})
module.exports = model("Member", Member, "Member")
公司:
const {Schema, model} = require('mongoose')
const Mongoose = require('mongoose')
const Corp = Schema ({
corpId: String,
name: String,
level:{
type: Number,
default: 1
},
members: [{
type: Mongoose.Types.ObjectId, ref: "Member"
}],
battlegroups: [{type: Mongoose.Types.ObjectId, ref: "Battlegroup"}]
})
module.exports = model("Corp", Corp, "Corp")
所以我原来只有2个文档。公司和成员。成员在Corp变量上具有Corp的_id,这是Schema Corp的引用。 问题是,以后,某人应该能够更改其当前公司,因此应该将其都从该公司的成员引用列表中删除,并且新的成员文档中的该公司引用也应进行更改。公司。
async function LeaveCorporation(targetb, message, MemberDataResult) {
let NewCorporation
let OldCorporation
await (CorpModel.findOne({corpId: message.guild.id.toString()}, (err, LeftOne) => {
if(err) return console.log(err)
OldCorporation = LeftOne
setTimeout(removeTheWeak, 6000, OldCorporation, MemberDataResult)
}))
await (CorpModel.findOne({corpId: "-1"}, (err, ObtainedOne) => {
if(err) return console.log(err)
if(!ObtainedOne) {
Corporation = new CorpModel({
_id: new Mongoose.Types.ObjectId(),
name: "The Forgotten Ones of Moria",
corpId: "-1"
})
Corporation.save()
NewCorporation = Corporation
setTimeout(assignNewCorp, 6000, targetb, message, NewCorporation, MemberDataResult)
}
else {
NewCorporation = ObtainedOne
setTimeout(assignNewCorp, 6000, targetb, message, NewCorporation, MemberDataResult)
}
}))
}
async function assignNewCorp(target, message, corp, MemberDataResult) {
MemberModel.findOneAndUpdate({discordId: target.id.toString()}, {Corp: corp._id})
message.channel.send("You've left!")
}
async function removeTheWeak(OldCorporation, MemberDataResult) {
let newMemberList = OldCorporation.members.filter(member => member != MemberDataResult._id)
setTimeout(Cleanse, 6000, OldCorporation, newMemberList)
}
async function Cleanse(OldCorporation, newMemberList) {
CorpModel.findOneAndUpdate({corpId: OldCorporation.corpId}, {$set: {members: newMemberList}},
{ safe: true, upsert: true }).catch(err => console.log(err))
}
但是在这段代码中,我没有任何错误。它遍历了所有内容,但它不会更改存储在成员文档中的Corp._id,也不会从Corp架构中的members数组中删除该成员。
有人知道为什么会这样吗?即使在控制台上,我也没有任何错误。