类型错误:db.collection(...).save 不是函数

Cal*_*leb 1 mongodb node.js express

我正在尝试使用 html 表单提交数据并使用 nodejs/expres crud 操作将数据发送到我的 mongodb 数据库,但我收到错误:

类型错误:db.collection(...).save 不是函数

我有两个文件,server.js 和index.html。

索引.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>
Run Code Online (Sandbox Code Playgroud)

我的 mongodb 数据库是“notesDB”,集合是“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('/')
    })
  })
Run Code Online (Sandbox Code Playgroud)

标题中的错误关于堆栈溢出还有很多其他解决方案,最流行的是

这是您在项目中安装的 mongodb 版本错误。您应该在项目中运行 npm install mongodb@2.2.5 --save

当我尝试这个时,我的代码中有 4 个严重漏洞以及多个其他错误,我意识到我应该只使用最新版本,所以我不喜欢这个解决方案。

我尝试的第二个解决方案是移动 crud 操作,即 mongodb 连接下的 get 和 post,我尝试这样做是因为有关此错误的其他帖子都是以这种方式构造的,这没有改变任何内容:

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('/')
        })
    })

})
Run Code Online (Sandbox Code Playgroud)

我阅读了更多有关使用客户端而不是数据库的信息,因此我用客户端替换了数据库并尝试了这种方式,这导致了相同的错误。自从我从教程中获得了这段代码后,我就从他的 github 下载了代码的最终版本并启动了该项目,它工作了,因此我正在格外努力地寻找为什么它不起作用,因为它绝对应该工作,如果有帮助的话,代码就在这里,因为代码非常相似但实际上有效。

编辑:我正在 3000 上收听,它提交了我收到错误的表格,只是为了澄清。

小智 8

.save() 已被弃用,而不是 .save() 您可以使用 .insertOne() 或 .insertMany() 或 .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('/')
    })
  })
Run Code Online (Sandbox Code Playgroud)

对于进一步的 CRUD 操作,您可以按照以下文档进行操作

https://www.mongodb.com/developer/quickstart/node-crud-tutorial/?_ga=2.210725788.862450187.1649763419-1339553468.1649528473&_gac=1.209527078.1649528473.EAIaIQobChMIt8KGr8 yH9wIVg3ZgCh3-2gcdEAAYASABEgJH8vD_BwE