我正在嘗試使用 nodejs express 腳本連接到我的后端 mongodb 5.0.5。使用 nodemon 運行 app.js 時,它顯示您的 mongodb 已連接,但在 localhost 上它繼續重新加載,沒有任何錯誤堆疊跟蹤。
我在模型中使用貓鼬模型,就像在 nodejs 中的 MVC 中一樣。不知道為什么它沒有在 localhost 上運行,當我上次使用這些檔案時它作業正常。
這是我的檔案:
應用程式.js
// imports
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const { append } = require('express/lib/response')
const zomatoRoutes = require('./Routes/zomato')
const mongoose = require('mongoose')
// create express server
var app = express()
//listen to a port
app.listen(7878, () => {
console.log('app running on port 7878');
})
app.use(bodyParser.json())
//connect mongodb
mongoose.connect('mongodb://localhost/zomato', () => {
console.log("MongoDB Connected");},
e => console.log(e)
)
// Middleware routes
app.use('/zomato', zomatoRoutes)
app.use(cors())
模型 > 位置.js
const mongoose = require('mongoose')
const locationSchema = new mongoose.Schema({
name: {
type: String,
required:true
},
city_id: {
type: String,
required:true
},
location_id: {
type: String,
required:true
},
country_name: {
type: String,
required:true
}
})
module.exports = mongoose.model("LocationsModel", locationSchema, "locations")
控制器 > location.js
const { status } = require('express/lib/response')
const Locations = require('../Model/location')
exports.getAllLocations = (req, res) => {
Locations.find().then(
result => {
res.status(200).json({ message: "data fetched successfully", data: result })
}
).catch(error => {
res.send(500).json({ message: "Error in Database", error: error })
})
}
和我的路線
zomato.js
const express = require('express')
const Router = express.Router();
const RestaurantController = require('../Controller/Restaurant')
const LocationsController = require('../Controller/Location')
// configure routes
// Restaurants routes
Router.get('/restaurants', RestaurantController.getAllRestaurants)
Router.post('/restaurants/filter/:pageNo', RestaurantController.getRestaurantsByFilter)
// Locations routes
Router.get('/locations', LocationsController.getAllLocations)
module.exports = Router
我的 Json 檔案 location.json 是這樣的:
[
{
"_id": "1",
"name": "ShalimarBhagh, Delhi",
"city_id": "1",
"location_id": "1",
"country_name": "India"
},
{
"_id": "2",
"name": "Janpat, Delhi",
"city_id": "1",
"location_id": "2",
"country_name": "India"
},
{
"_id": "3",
"name": "MSP, Delhi",
"city_id": "1",
"location_id": "3",
"country_name": "India"
}
]
** 更新:我忘了提到我最近更新到 windows10 以確保我的 react 應用程式正常作業,并且在出現此問題后,現在我創建了一個新的應用程式,洗掉了 mongo 并重新安裝了 mongodb 仍然在郵遞員錯誤中得到這個錯誤:閱讀 ECONNRESET**

更新我得到這個堆疊跟蹤
C:\Users\acer\Documents\EDUREKA\Nodejs-mongodb-
mongoose\node_modules\mongoose\lib\connection.js:797
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri
(C:\Users\acer\Documents\EDUREKA\Nodejs-mongodb-
umongoose\node_modules\mongoose\lib\connection.js:797:32)
uj5u.com熱心網友回復:
試試這個:
// imports
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const { append } = require('express/lib/response')
const zomatoRoutes = require('./Routes/zomato')
const mongoose = require('mongoose')
// create express server
const app = express()
// Middleware routes
app.use(cors())
app.use(bodyParser.json())
app.use('/zomato', zomatoRoutes)
//connect mongodb
mongoose.connect('mongodb://localhost/zomato', () => {
console.log("MongoDB Connected");
const server = app.listen(7878, () => {
console.log('app running on port 7878');
})
server.setTimeout(0) // disable the default time out server mechanism
},
e => console.log(e)
)
這是:
- 初始化
exprress應用 - 注冊中間件
- 連接到 mongo
- 如果連接成功(并且僅在那時) - 啟動應用程式
另外,替換您的代碼
res.status(200).json({ data: 'whatever' })
到:
res.status(200).send({ data: 'whatever' })
您的回應有可能沒有回傳給客戶端,因此 - 導致超時錯誤。這種方法也應該解決這個問題
希望它能幫助你解決你的問題
uj5u.com熱心網友回復:
本地主機未解決,盡管我使用 mongod --ipv6 將我的 mongodb 更改為 ipv6,但它仍然不接受,并且在堆疊跟蹤中顯示 isIPV6:false。
所以最后我不得不改變使用這里的答案使用 ipv4 監聽埠
//connect to mongoDB
const uri = 'mongodb://localhost:27017/zomato';
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
family:4
}
const connectWithDB = () => {
mongoose.connect(uri, options, (err, db) => {
if (err) console.error(err);
else console.log("database connection")
})
}
connectWithDB()
它現在正在作業并獲取資料,但想解決 ipv6 nodejs 的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/417376.html
標籤:
上一篇:無法在“視窗”上執行“獲取”:無法從http://app-name.herokuapp.com:undefined/api/auth/login決議URL
