连接到mongodb云时如何解决ETIMEOUT错误?

时间:2019-09-26 14:38:54

标签: node.js mongodb mongoose

这是我尝试与Mongo Cloud建立连接的地方。

const mongoose = require("mongoose");
 // DB Config
    const db = require("./config/keys").mongoURI;
    // Connect Mongo
    mongoose
      .connect(db, { useNewUrlParser: true })
      .then(() => console.log("Mongo is connected"))
      .catch(err => console.log(err));

这是我用来在DB中注册用户的途径。

router.post("/register", (req, res) => {
  User.findOne({ email: req.body.email }).then(user => {
    if (user) {
      return res.status(400).json({ email: "email already exists" });
    } else {
      const avatar = gravatar.url(req.body.email, {
        s: "200", //size
        r: "pg", //rating
        d: "mm" //default
      });

      const newUser = new User({
        name: req.body.name,
        email: req.body.email,
        avatar,
        password: req.body.password
      });
      bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
          if (err) throw err;
          newUser.password = hash;
          newUser
            .save()
            .then(user => res.json(user))
            .catch(err => console.log(err));
        });
      });
    }
  });
});

这是我尝试连接到Mongo Cloud的错误。

{ Error: querySrv ETIMEOUT _mongodb._tcp.devconnecter-n1ij2.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:197:19)
  errno: 'ETIMEOUT',
  code: 'ETIMEOUT',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.devconnecter-n1ij2.mongodb.net' }

0 个答案:

没有答案