猫鼬错误无法连接到服务器[locahost:27017]

时间:2019-11-27 13:07:07

标签: node.js mongodb express mongoose server-side

我在Windows上遇到了mongodb的问题,我在单独的外壳中打开mongod.exe,然后在第二个外壳中运行mongo.exe并进入服务器文件夹并运行

nodemon server.js

然后我得到这个错误。

    (node:9276) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [locahost:27017] on first connect 
    [Error: getaddrinfo ENOTFOUND locahost at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26){
    name: 'MongoNetworkError',
    errorLabels: [Array],
    [Symbol(mongoErrorContextSymbol)]: {}
    }]

\node_modules\mongodb\lib\core\topologies\server.js:433:11
at Pool.emit (events.js:210:5)
\node_modules\mongodb\lib\core\connection\pool.js:562:14
\node_modules\mongodb\lib\core\connection\pool.js:985:11
\node_modules\mongodb\lib\core\connection\connect.js:40:11
\node_modules\mongodb\lib\core\connection\connect.js:262:5)
\node_modules\mongodb\lib\core\connection\connect.js:287:7)
at Object.onceWrapper (events.js:300:26)
at Socket.emit (events.js:210:5)
at emitErrorNT (internal/streams/destroy.js:84:8)
processTicksAndRejections (internal/process/task_queues.js:80:21)

之后

(node:9276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是server.js的样子

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var db = mongoose.connect("mongodb://locahost/swag-shop");

var Product = require("./model/product");
var WishList = require("./model/wishlist");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.post("./product", (req, res) => {
  var product = new Product();
  product.title = req.body.title;
  product.price = req.body.price;
  product.save(function(err, savedProduct) {
    if (err) {
      Response.status(500).send({ error: "Could not save product" });
    } else {
      Response.send(savedProduct);
    }
  });
});

app.listen(3000, () => {
  console.log("Swag shop API running on port 3000...");
});

这是product.js

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var product = new Schema({
  title: String,
  price: Number,
  likes: { tpye: Number, default: 0 }
});

module.exports = mongoose.model("Product", product);

然后是wishlist.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;

var wishList = new Schema({
    title: {type: String, default: "Cool Wish List"},
    products: [{type: ObjectId, ref:'Product'}]
})

module.exports = mongoose.model('WishList', wishList);

编辑:尝试发布到http://localhost:3000/product时,邮递员的状态为404

3 个答案:

答案 0 :(得分:0)

app.POST('/product')删除与其他文件的.数据库连接定义并引用它。跟随-

server.js

var express = require("express");
var app = express();
var bodyParser = require("body-parser");

var Product = require("./model/product");
var WishList = require("./model/wishlist");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.post("/product", (req, res) => {
  var product = new Product();
  product.title = req.body.title;
  product.price = req.body.price;
  product.save(function(err, savedProduct) {
    if (err) {
      Response.status(500).send({ error: "Could not save product" });
    } else {
      Response.send(savedProduct);
    }
  });
});

app.listen(3000, () => {
  console.log("Swag shop API running on port 3000...");
});

modelsConnection.js

var mongoose = require("mongoose");
mongoose.connect("mongodb://locahost/swag-shop");

module.exports = { mongoose };

product.js

const { mongoose } = require('../modelsConnection');
var Schema = mongoose.Schema;

var product = new Schema({
  title: String,
  price: Number,
  likes: { tpye: Number, default: 0 }
});

module.exports = mongoose.model("Product", product);

wishlist.js

const { mongoose } = require('../modelsConnection');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;

var wishList = new Schema({
    title: {type: String, default: "Cool Wish List"},
    products: [{type: ObjectId, ref:'Product'}]
})

module.exports = mongoose.model('WishList', wishList);

答案 1 :(得分:0)

尝试从帖子设置中删除点 app.post("./product", (req, res) => {

答案 2 :(得分:0)

我认为您到目前为止已经找到问题的答案。但是,也许您可​​以在“ server.js”中放入res而不是Response。

正确的代码:

if (err) {
    res.status(500).send({ error: "Could not save product" });
} else {
    res.send(savedProduct);
}