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

问题描述 投票:0回答:2

[我在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)]: {}
    }]

at

\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状态

node.js mongodb express mongoose server-side
2个回答
0
投票

[app.POST('/product')删除对其他文件的. db连接定义并引用该文件。关注-

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);

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.