您如何使用 mongodb native、express 和 body-parser 来 post 请求和保存数据?
How do you use mongodb native, express and body-parser to post request and save data?
我一直在尝试将数据保存到 mongodb 数据库,但我所做的一切似乎都不起作用。我遇到的第一个错误与 req.body
有关
当我点击提交按钮时,console.log(req.body)
returns
[Object: null prototype] { name: 'John', priority: 'go to bed' }
而不是
{ name: 'John', priority: 'go to bed' }
其次,我不知道我是否正确地将数据保存到数据库中,因为我看到了太多不同的方法,所以我很困惑
相关代码行
db.collection.insertOne(req.body);
及相关错误:
TypeError: db.createCollection is not a function
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = "mongodb://localhost:27017";
app.listen(7000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
})
app.post('/todo',urlencodedParser,function(req, res){
MongoClient.connect(url, { useNewUrlParser: true }, function(err,db){
if(err) throw err;
console.log('Databese created!');
db.collection.insertOne(req.body);
db.close();
});
console.log(req.body);
});
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
I am an index page.
<form class="" action="/todo" method="post">
<input type="text" name="name" placeholder="todo">
<input type="text" name="priority" placeholder="priority">
<button type="submit">Submit</button>
</form>
</body>
</html>
你的问题是你没有为你的MongoDB实例指定你正在使用哪个数据库。
const url = "mongodb://localhost:27017/myDataBaseName";
这应该可以为您解决这个问题。
关于第一个错误,您应该添加这两行以获取 json 数据作为请求对象,因为您要在 json[ 中发送数据=12=]
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
关于第二个查询:
在 mongoDB
中插入记录
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
我一直在尝试将数据保存到 mongodb 数据库,但我所做的一切似乎都不起作用。我遇到的第一个错误与 req.body
当我点击提交按钮时,console.log(req.body)
returns
[Object: null prototype] { name: 'John', priority: 'go to bed' }
而不是
{ name: 'John', priority: 'go to bed' }
其次,我不知道我是否正确地将数据保存到数据库中,因为我看到了太多不同的方法,所以我很困惑
相关代码行
db.collection.insertOne(req.body);
及相关错误:
TypeError: db.createCollection is not a function
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = "mongodb://localhost:27017";
app.listen(7000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
})
app.post('/todo',urlencodedParser,function(req, res){
MongoClient.connect(url, { useNewUrlParser: true }, function(err,db){
if(err) throw err;
console.log('Databese created!');
db.collection.insertOne(req.body);
db.close();
});
console.log(req.body);
});
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
I am an index page.
<form class="" action="/todo" method="post">
<input type="text" name="name" placeholder="todo">
<input type="text" name="priority" placeholder="priority">
<button type="submit">Submit</button>
</form>
</body>
</html>
你的问题是你没有为你的MongoDB实例指定你正在使用哪个数据库。
const url = "mongodb://localhost:27017/myDataBaseName";
这应该可以为您解决这个问题。
关于第一个错误,您应该添加这两行以获取 json 数据作为请求对象,因为您要在 json[ 中发送数据=12=]
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
关于第二个查询: 在 mongoDB
中插入记录MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});