我正在尝试构建游戏,并且很难弄清为什么userInfo.create无法正常工作。基本上,我将它设置为用户登录的位置,并发送获取请求以获取其个人资料信息。如果不存在,请创建它。我不得不分两部分来执行此操作。首先是一个非常基本的个人资料信息,其中包含名称,电子邮件,ID等,并且效果很好。问题出在第二个请求中,该请求在第一个请求之后触发,因为它取决于userId来检查他们是否有配置文件。由于某些原因,它只是不想创建信息。所有这些都是从猫鼬模式构建的。一切似乎都与基本配置文件信息相同。至于架构本身,我要传递的只是userId。其他所有内容均设置为必填:true并且已经设置了默认值。对于可能还需要什么,我有点茫然。
损坏的getUserInfo:
async getUserInfo(id) {
let info = await dbContext.UserInfo.findOne({
_id: id
})
if (info == null) {
// let obj = UserInfo
let obj = {}
obj.userId = id
let newUserInfo = await dbContext.UserInfo.create(obj)
return newUserInfo
} else { return info }
正在使用getProfile:
async getProfile(user) {
let profile = await dbContext.Profile.findOne({
email: user.email
});
profile = await createProfileIfNeeded(profile, user);
await mergeSubsIfNeeded(profile, user);
return profile;
}
模式:
import mongoose from "mongoose";
const Schema = mongoose.Schema;
const UserInfo = new Schema(
{
userId: { type: String, required: true, unique: true },
resources: {
gold: { type: Number, required: true, default: 0 },
food: { type: Number, required: true, default: 0 },
ore: { type: Number, required: true, default: 0 },
wood: { type: Number, required: true, default: 0 }
},
upgrades: {
arrows: {
name: { type: String, required: true, defualt: "StoneArrow" },
count: { type: Number, required: true, default: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
ore: { type: Number, required: true, default: 50 },
wood: { type: Number, required: true, defualt: 100 }
},
nameArr: ["StoneArrow", "BronzeArrow", "IronArrow", "SteelArrow", "DiamondArrow"]
},
axes: {
name: { type: String, required: true, defualt: "StoneAxe" },
count: { type: Number, required: true, default: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
ore: { type: Number, required: true, default: 50 },
wood: { type: Number, required: true, defualt: 100 }
},
nameArr: ["StoneAxe", "BronzeAxe", "IronAxe", "SteelAxe", "DiamondAxe"]
},
picks: {
name: { type: String, required: true, defualt: "StonePick" },
count: { type: Number, required: true, default: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
ore: { type: Number, required: true, default: 50 },
wood: { type: Number, required: true, defualt: 100 }
},
nameArr: ["StonePick", "BronzePick", "IronPick", "SteelPick", "DiamondPick"]
},
},
buildings: {
blacksmithShop: {
count: { type: Number, required: true, defualt: 1 },
cost: {
gold: { type: Number, required: true, default: 1000 },
ore: { type: Number, required: true, default: 1200 },
wood: { type: Number, required: true, default: 1500 }
}
},
tavern: {
count: { type: Number, required: true, defualt: 1 },
cost: {
gold: { type: Number, required: true, default: 1000 },
ore: { type: Number, required: true, default: 1200 },
wood: { type: Number, required: true, default: 1500 }
}
}
},
hires: {
hunter: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
},
miner: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
},
lumberjack: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
},
trader: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
},
builder: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
},
blacksmith: {
count: { type: Number, required: true, defualt: 0 },
cost: {
gold: { type: Number, required: true, default: 200 },
food: { type: Number, required: true, default: 1000 },
}
}
}
},
// { timestamps: true, toJSON: { virtuals: true } }
)
export default UserInfo
我可以根据需要提供更多信息,一如既往,我们非常感谢您提供任何反馈意见。谢谢!
答案 0 :(得分:0)
我不确定为什么,但是我不得不将.create别名为另一个函数,它现在似乎可以正常工作。