摘要
在Mongoose中有一个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);
答案 0 :(得分:0)
看起来像是问题所在,因为彼此需要fortniteUsers
和users
,所以当在另一个内部调用时,其中一个尚未导出。
要验证这一点,您可以在要求console.log(FortniteUsers)
和console.log(Users)
之后使用它们。您可以看到FortniteUsers
是Model {FortniteUsers}
,但是Users
只是{}
。
您可以通过将const FortniteUsers = require("./fortniteUsers");
这一行移到users.js
中的导出之后来解决它(不确定这是好方法,但是它可以工作)。