[英]Unable to connect with MongoDb using nodeJS
I am creating MERN
stack app I am trying to connect my app with MongoDb
database.I am using Mongoose
library to connect with `MongoDb.I am getting error below:我正在创建
MERN
堆栈应用程序我正在尝试将我的应用程序与MongoDb
数据库连接。我正在使用Mongoose
库与`MongoDb 连接。我收到以下错误:
Error错误
TypeError: callback is not a function ['server] at $initialConnection.$initialConnection.then (H:\\React projects\\MyBlog\\node_modules\\mongoose\\lib\\connection.js:744:13) ['server] at process._tickCallback (internal/process/next_tick.js:68:7)
TypeError: callback is not a function ['server] at $initialConnection.$initialConnection.then (H:\\React projects\\MyBlog\\node_modules\\mongoose\\lib\\connection.js:744:13) ['server] at process._tickCallback (内部/进程/next_tick.js:68:7)
Here is my code below:这是我的代码如下:
server.js服务器.js
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(require('./routes/saveData.js'));
const port = process.env.PORT || 5000;
app.listen(port,() => {
console.log(`Server is running on ${port}.`);
});
saveData.js保存数据.js
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const blogs = require('../models/blogPost');
const mongoose = require('mongoose');
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended: true}));
const dburl = 'mongodb+srv://example:example@cluster1-5wp.mongodb.net/ExpDb?retryWrites=true'
router.post('/save',(req,res) => {
const data = {
title: req.body.title,
detail: req.body.detail
}
const newBlog = new blogs(data);
mongoose.connect(dburl, {useNewUrlParser: true,useUnifiedTopology:true}).then((resp) =>{
resp.send("Connected");
console.log("connected");
}).catch((err) => {
console.log("database error: ",err);
});
});
module.exports = router;
Below is mongoose schema: blogPost.js下面是猫鼬模式: blogPost.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BlogPostSchema = new Schema({
title:String,
body:String,
date:{
type:String,
dafault:Date.now()
}
});
const BlogPost = mongoose.model('BlogPost',BlogPostSchema);
module.exports = BlogPost;
Someone please let me know what I am doing wrong.Any help would be appreciated.有人请让我知道我做错了什么。任何帮助将不胜感激。
THANKS谢谢
Mongoose returned promise value is resp
. Mongoose 返回的 promise 值是
resp
。 Server response object which is having .send
method is res
.具有
.send
方法的服务器响应对象是res
。 Try below :试试下面:
router.post('/save', (req, res) => {
...
mongoose.connect(dburl, {useNewUrlParser: true, useUnifiedTopology:true}).then((resp) =>{
// res not resp
res.send("Connected");
console.log("connected");
})
Syntax from the documentation,文档中的语法,
mongoose.connect(uri, options).then(
() => { /** ready to use. The `mongoose.connect()` promise resolves to mongoose instance. */ },
err => { /** handle initial connection error */ }
);
Code代码
router.post('/save',(req,res) => {
mongoose.connect(dburl, {useNewUrlParser: true,useUnifiedTopology:true})
.then(() =>{
res.send("Connected");
console.log("connected");
},
(err)=> console.log("database error: ",err);)
});
}
mongoose connect doesn't have a callback猫鼬连接没有回调
mongoose.connect(dburl, {useNewUrlParser: true},{useUnifiedTopology:true});
this should work.这应该有效。 And then to update you should use the mongoose save method on the model object.
然后更新你应该使用模型对象上的猫鼬保存方法。
BlogPost.save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.