Mongoose - 不能在 post findOneAndUpdate 钩子中调用 model.find()
Posted
技术标签:
【中文标题】Mongoose - 不能在 post findOneAndUpdate 钩子中调用 model.find()【英文标题】:Mongoose - can't call model.find() in post findOneAndUpdate hook 【发布时间】:2019-12-26 02:24:29 【问题描述】:总结
在 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);
【问题讨论】:
【参考方案1】:看起来问题是因为您需要 fortniteUsers
和 users
彼此,因此在另一个内部调用时其中一个尚未导出。
要验证这一点,您可以在需要它们之后 console.log(FortniteUsers)
和 console.log(Users)
。你可以看到FortniteUsers
是Model FortniteUsers
但Users
只是。
您可以通过将这一行 const FortniteUsers = require("./fortniteUsers");
移动到 users.js
中的导出之后来解决它(不确定这是不是好方法,但它有效)。
【讨论】:
以上是关于Mongoose - 不能在 post findOneAndUpdate 钩子中调用 model.find()的主要内容,如果未能解决你的问题,请参考以下文章
Mongoose .save() 不能在没有回调的情况下工作
无法在 POST (mongoose/express) 上创建新文档
如何使用 mongoose 和 nodejs 删除嵌套在 Post 模式中的评论?