我正在尝试使用 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, …Run Code Online (Sandbox Code Playgroud)