首页 > 解决方案 > 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);

标签: javascriptnode.jsmongodbmongoose

解决方案


看起来问题是因为您需要fortniteUsersusers彼此,所以当在另一个内部调用时,其中一个尚未导出。

要验证这一点,您可以console.log(FortniteUsers)要求console.log(Users)它们。你可以看到FortniteUsersis Model {FortniteUsers}but Usersis just {}

您可以通过将这条线移动const FortniteUsers = require("./fortniteUsers");到导出之后来解决它users.js(不确定这是不是好方法,但它有效)。


推荐阅读