Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Next.JS和Mongoose模型_Node.js_Mongodb_Next.js_Mongoose Schema - Fatal编程技术网

Node.js Next.JS和Mongoose模型

Node.js Next.JS和Mongoose模型,node.js,mongodb,next.js,mongoose-schema,Node.js,Mongodb,Next.js,Mongoose Schema,我正在为一个客户端构建一个非常简单的next.js应用程序,就我的一生而言,我无法理解为什么编译后无法覆盖User模型。 每次我关闭开发服务器时,对于任何路由的第一个请求:addUser、authenticate、GET。。。它只在第一次请求时起作用。这是我连接MongoDB的功能 const connectDB = (handler) => async (req, res) => { if (mongoose.connections[0].readyState) { r

我正在为一个客户端构建一个非常简单的next.js应用程序,就我的一生而言,我无法理解为什么编译后无法覆盖
User
模型。 每次我关闭开发服务器时,对于任何路由的第一个请求:addUser、authenticate、GET。。。它只在第一次请求时起作用。这是我连接MongoDB的功能

const connectDB = (handler) => async (req, res) => {
  if (mongoose.connections[0].readyState) {
    return handler(req, res);
  }
  await mongoose
    .connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
    })
    .then(() => {
      return handler(req, res);
    });
};

const middleware = nc();
middleware.use(connectDB);
// middleware.use(authMiddleware);
module.exports = {
  connectDB,
  middleware,
};
模型:

const mongoose = require("mongoose");
const bcrypt = require("bcryptjs");

const UserSchema = mongoose.Schema(
  {
    userName: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
  },
  { timestamps: true }
);

UserSchema.pre("save", async function (next) {
  if (this.isModified("password")) {
    const salt = await bcrypt.genSalt(10);
    this.password = await bcrypt.hash(this.password, salt);
    next();
  } else {
    next(new Error("failed to encrypt password"));
  }
});

UserSchema.methods.comparePassword = async function (password, next) {
  const comparison = bcrypt.compare(password, this.password);
  if (!comparison) {
    return {
      isMatch: false,
      comparison: comparison,
    };
  } else {
    return {
      isMatch: true,
      comparison: comparison,
    };
  }
};

module.exports = mongoose.model("User", UserSchema);
路线:

const handler = nc();
// handler.use(middleware);
handler.post(async (req, res) => {
  console.log(colors.bgBlue("Did run in route..."));
  try {
    const user = await User.findOne({ userName: req.body.email });
    const { isMatch } = await user.comparePassword(req.body.password);
    if (!isMatch) {
      return res.status(401).json({ error: "These passwords do not match." });
    }
    if (isMatch) {
      const payload = {
        user: {
          id: user.id,
        },
      };
      jwt.sign(
        payload,
        process.env.JWT_SECRET,
        {
          expiresIn: 3600,
        },
        (err, token) => {
          if (err) throw err;
          return res.json({ userID: user._id, token });
        }
      );
    }
  } catch (error) {
    console.log(error);
    res.status(500).json({ error: "There was an error adding that user." });
  }
});

export default connectDB(handler);

很抱歉这么长时间,我真的不知道从哪里开始。我错过了express,但是SSR非常复杂:(

当你调用
comparePassword
时,你正在用
User
覆盖
User

将其更改为
User.comparePassword

说明:
user
是您声明的保存返回文档的变量,而
user
代表您从mongoose导入\需要的模型