TypeError: db.collection(...).save is not a function
TypeError: db.collection(...).save is not a function
我正在尝试使用 html 表单提交数据并使用 nodejs/expres crud 操作将数据发送到我的 mongodb 数据库,但我收到错误:
TypeError: db.collection(...).save is not a function
我有两个文件,server.js 和 index.html。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/quotes" method="POST">
<input type="text" placeholder="name" name="name">
<input type="text" placeholder="quote" name="quote">
<button type="submit">Submit</button>
</form>
</body>
</html>
我的 mongodb 数据库是“notesDB”,collection 是“quotes”
server.js:
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient
var db
MongoClient.connect('mongodb+srv://username:password@cluster0.ydyhg.mongodb.net/notesDB', (err, database) => {
if (err) return console.log(err)
db = database.db('notesDB')
app.listen(3000, () => {
console.log('listening on 3000')
})
})
app.use(bodyParser.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.post('/quotes', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
标题中的错误还有很多其他关于堆栈溢出的解决方案,最流行的是
It's mongodb version error which you have installed in your project.
You should run npm install mongodb@2.2.5 --save in your project
当我尝试这个时,我的代码中有 4 个严重漏洞和多个其他错误,我意识到我应该只使用最新版本,所以我不喜欢那个解决方案。
我尝试的第二个解决方案是移动 crud 操作,即 mongodb 连接下的 get 和 post,我尝试这个是因为其他 posts 关于这个以这种方式构建的错误,并没有改变任何东西:
MongoClient.connect('mongodb+srv://username:password@cluster0.ydyhg.mongodb.net/notesDB', (err, database) => {
if (err) return console.log(err)
db = database.db('notesDB')
app.listen(3000, () => {
console.log('listening on 3000')
})
app.use(bodyParser.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.post('/quotes', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
})
我阅读了一些关于使用客户端而不是数据库的更多信息,因此我用客户端替换了数据库并尝试了这种方式,这导致了同样的错误。
由于我从教程中获得了这段代码,所以我开始从他的 github 下载代码的最终版本并启动项目并且它有效,因此我非常努力地寻找为什么它不起作用,因为它绝对应该是,代码是 here 如果有帮助的话,因为代码非常相似但实际上有效。
编辑:我正在监听 3000,它提交的表格我收到了错误,只是为了澄清。
.save() 已被弃用而不是 .save() 你可以使用 .insertOne() 或 .insertMany() or.updateOne({upsert:true})
您可以通过将 .save() 替换为 .insertOne()
来编写如下代码
app.post('/quotes', (req, res) => {
db.collection('quotes').insertOne(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
进一步的CRUD操作可以参考下面的文档
我正在尝试使用 html 表单提交数据并使用 nodejs/expres crud 操作将数据发送到我的 mongodb 数据库,但我收到错误:
TypeError: db.collection(...).save is not a function
我有两个文件,server.js 和 index.html。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/quotes" method="POST">
<input type="text" placeholder="name" name="name">
<input type="text" placeholder="quote" name="quote">
<button type="submit">Submit</button>
</form>
</body>
</html>
我的 mongodb 数据库是“notesDB”,collection 是“quotes” server.js:
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient
var db
MongoClient.connect('mongodb+srv://username:password@cluster0.ydyhg.mongodb.net/notesDB', (err, database) => {
if (err) return console.log(err)
db = database.db('notesDB')
app.listen(3000, () => {
console.log('listening on 3000')
})
})
app.use(bodyParser.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.post('/quotes', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
标题中的错误还有很多其他关于堆栈溢出的解决方案,最流行的是
It's mongodb version error which you have installed in your project. You should run npm install mongodb@2.2.5 --save in your project
当我尝试这个时,我的代码中有 4 个严重漏洞和多个其他错误,我意识到我应该只使用最新版本,所以我不喜欢那个解决方案。
我尝试的第二个解决方案是移动 crud 操作,即 mongodb 连接下的 get 和 post,我尝试这个是因为其他 posts 关于这个以这种方式构建的错误,并没有改变任何东西:
MongoClient.connect('mongodb+srv://username:password@cluster0.ydyhg.mongodb.net/notesDB', (err, database) => {
if (err) return console.log(err)
db = database.db('notesDB')
app.listen(3000, () => {
console.log('listening on 3000')
})
app.use(bodyParser.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.post('/quotes', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
})
我阅读了一些关于使用客户端而不是数据库的更多信息,因此我用客户端替换了数据库并尝试了这种方式,这导致了同样的错误。 由于我从教程中获得了这段代码,所以我开始从他的 github 下载代码的最终版本并启动项目并且它有效,因此我非常努力地寻找为什么它不起作用,因为它绝对应该是,代码是 here 如果有帮助的话,因为代码非常相似但实际上有效。
编辑:我正在监听 3000,它提交的表格我收到了错误,只是为了澄清。
.save() 已被弃用而不是 .save() 你可以使用 .insertOne() 或 .insertMany() or.updateOne({upsert:true})
您可以通过将 .save() 替换为 .insertOne()
来编写如下代码app.post('/quotes', (req, res) => {
db.collection('quotes').insertOne(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
})
进一步的CRUD操作可以参考下面的文档