javascript - Mongoose - 不能在 post findOneAndUpdate 钩子中调用 model.find()
问题描述
概括
在 Mongoose 中有一个 post findOneAndUpdate 钩子,在这个钩子里面我需要查询数据库。我正在尝试在另一个模型上执行 .find() ,但是每次执行时,都会出现以下错误:
错误
TypeError: Users.find is not a function
at model.Query.<anonymous> (/Users/benjilightstone/Coding/eSports-node-RESTful-API/api/models/fortniteUsers.js:29:32)
我尝试在我的一条快速路线中运行完全相同的 .find() 并且它运行良好。我已经检查了用户的 require() 并且它已正确导入。我不知道为什么我会收到这个错误,并且希望得到一些帮助!
代码
fortniteUsers.js(带有后挂钩的模型)
const mongoose = require("mongoose");
const Users = require("./users");
const uniqueValidator = require("mongoose-unique-validator");
const statsFieldsSchema = require("./statsFields");
const fnUserPlatformSchema = {
//this is so that have a unique entry for a username &platform combo
fnUser: { type: String, require: true },
platform: { type: String, enum: ["pc", "xb1", "psn"], require: true }
};
//TODO took out updated at because the record shouldn't get updated?
//TODO think about usecase of Fortnite user changing their username
const fortniteUserSchema = mongoose.Schema({
// pass a javascript object that defines the schema
fnUserPlatform: { type: fnUserPlatformSchema, unique: true, require: true },
createdAt: { type: Date, require: true },
hasAccount: { type: Boolean, require: true, default: false },
updatedAt: { type: Date, require: false },
lifeTimeStats: { type: statsFieldsSchema, require: true }
});
fortniteUserSchema.post("findOneAndUpdate", async function(fortniteUser, next) {
try {
console.log("inside fortniteUser post hook", typeof fortniteUser._id);
const fnUserId = String(fortniteUser._id);
console.log("fnUserId", fnUserId);
// const userId = await User.find({ fnUser: fnUserId });
const userId = await Users.find({ fnUser: "5ccb08198f52f40117e950b3" });
console.log("userId", userId);
} catch (err) {
console.log("error in post hook", err);
}
});
fortniteUserSchema.plugin(uniqueValidator);
module.exports = mongoose.model("FortniteUser", fortniteUserSchema);
users.js(在 fortniteUsers.js 中导入的用户模型)
const mongoose = require("mongoose");
const FortniteUsers = require("./fortniteUsers");
require("mongoose-type-email");
const userSchema = mongoose.Schema({
// pass a javascript object that defines the schema
fnUser: {
type: mongoose.Schema.Types.ObjectId,
ref: "FortniteUser",
require: true
},
eSportsUsername: { type: String, require: true },
password: { type: String, require: true, minlength: 5 },
email: { type: mongoose.SchemaTypes.Email, require: true, unique: true },
ageConfirmed: { type: Boolean, require: true },
createdAt: { type: Date, require: true },
updatedAt: { type: Date, require: false }
});
userSchema.pre("save", function() {
console.log("inside pre save users.js");
console.log("docToSave", this); // QUESTION: how would I pass in req.body.fnUser
FortniteUsers.findOne({
fnUserPlatform: { username: req.body.fnUser, platform: "psn" }
})
.then(result => {
console.log("result in pre", result);
})
.catch(err => console.log("error i expect", err));
});
userSchema.post("save", function(user) {
//TODO where is user gettting passed from, I dont really understand this
console.log("we're in post save ... ", user);
FortniteUsers.findOneAndUpdate(
{ _id: user.fnUser },
{ $set: { hasAccount: true } },
function(err, doc) {
if (err) {
console.log("error in mongoose userSchema.post", err);
}
}
);
});
module.exports = mongoose.model("User", userSchema);
解决方案
看起来问题是因为您需要fortniteUsers
和users
彼此,所以当在另一个内部调用时,其中一个尚未导出。
要验证这一点,您可以console.log(FortniteUsers)
要求console.log(Users)
它们。你可以看到FortniteUsers
is Model {FortniteUsers}
but Users
is just {}
。
您可以通过将这条线移动const FortniteUsers = require("./fortniteUsers");
到导出之后来解决它users.js
(不确定这是不是好方法,但它有效)。
推荐阅读
- r - 如何添加行并通过多个变量推断数据?
- c# - 如何将我的 Windows 社区模板数据网格的数据项行上的集合用作同一行上 ComboBox 列的 Itemsource?
- python - 无法在 Python 3.7 或 Python 3.8 中导入 numpy - Ubuntu 18.04
- r - 在 R 中使用 read_excel 正确解析日期
- r - 如何删除R字符串中的“\”字符?
- swift - Swift 中 NSMutableArray 的排序函数的参数是什么?
- git - 删除损坏的 git 文件
- sql - 分区表中的分区日期范围
- c# - 有没有人有使用 NuGet 的 DocumentFormat.OpenXML.DotNet.Core 包构建控制台应用程序 (C#) 的经验?
- asp.net-web-api2 - 从旧版本中查找新版本 Postman 中的字段