React/节点Mongoose错误:操作“users.insertOne()"缓冲在10000 ms后超时

slsn1g29  于 2023-02-16  发布在  Go
关注(0)|答案(1)|浏览(125)

提前感谢您阅读本文。
我想先说我是react/node/mongoose和mongoDB的新手,如果我错过了一些非常基本的东西,我很抱歉,我只是在圣诞节断断续续地开发react,现在我决定给它添加一个node后端。一个将变量传递给节点后端的react表单。2当我的API决定尝试使用atlas托管的mongoDB来保存一些东西时,我遇到了一个问题。我从来没有遇到过这个问题,当在另一个节点应用程序中开发这个时,它似乎是间歇性的,直到我离开我的工作VPN,每次都插入这种情况,所以我认为很好,是时候把它带到我的react项目中了。这是目前的API外观(见下文,出于显而易见的原因,已经替换了DB常量中的用户名和密码,但我可以向您保证这些是正确的)

module.exports = {
  registerUser: async (username, password) => {
    //const User = require("./database/schema/userSchema.js");
    const mongoose = require("mongoose");

    const userSchema = new mongoose.Schema({
      username: {
        type: String,
        require: [true, "a user must have a username"],
        unique: true,
      },
      password: {
        type: String,
        require: [true, "a user must have a password"],
      },
    });

    var User = mongoose.model("User", userSchema);

    const DB =
      "mongodb+srv://<username>:<password>@cluster0.mh4jhqg.mongodb.net/tehcooky?retryWrites=true&w=majority";
    console.log("DB: " + DB);

    mongoose
      .connect(DB, {
        userNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: false,
        useUnifiedTopology: true,
      })
      .then(() => console.log("DB Connection Successful!"))
      .catch((err) => {
        console.log(err);
      });
    var message = "";
    var user = new User({
      username: username,
      password: password,
    
});
    console.log("user object: " + user);
    console.log("attempting to register user");
    await user
      .save()
      .then((doc) => {
        console.log(doc);
        message = "user data send to database";
        console.log("sent to database");
      })
      .catch((err) => {
        console.log("error 🤩: ", err);
        message = `error: ${err}`;
      });
    mongoose.disconnect();

    //const response = "nice";
    const response = `message: ${message}, user: ${username}, password: ${password}`;
    return response;
  },
};

我试过重置我的连接,改变我的dns,把模型命令后,然后db连接,并使它等待之前,它继续在大多数情况下。我也有我的IP存储在访问的地方atlas,以及接受所有连接从任何地方设置以防万一。
我想知道是否有人能帮助我,我确信我写的东西有一些明显的缺陷,因为我没有做任何整理,但就我所见,一切都很好,直到它命中保存,我假设这是一个承诺,mongoDB接管,然后可能有某种身份验证问题,试图实际插入数据?集合是根据图表命中

bqf10yzr

bqf10yzr1#

感谢您的建议,我今天给我们的解决方案架构师打了个电话,我们找到了一个解决方案,除了我需要清理代码和重构之外,就是这样了

module.exports = {
  registerUser: async (username, password) => {
    // const User = require("./database/schema/userSchema.js");
    const mongoose = require("mongoose");

    const userSchema = new mongoose.Schema({
      username: {
        type: String,
        require: [true, "a user must have a username"],
        unique: true,
      },
      password: {
        type: String,
        require: [true, "a user must have a password"],
      },
    });

    const User = mongoose.model("users", userSchema);
    const message = "";

    console.log("attempting to register user");

    const DB =
      connection string (taken out);
    console.log("DB: " + DB);

    const connection = await mongoose.connect(DB);
    if (!connection) {
      console.log("No connection");
      throw new Error("no connection");
    }
    const user = new User({
      username,
      password,
    });
    const data = await user.save();
    console.log("the data send back by promise is: " + data);
    await mongoose.disconnect();

    // const response = "nice";
    const response = `message: ${message}, user: ${username}, password: ${password}`;
    return response;
  },
};

相关问题