Javascript 使用mongodb提交后插入更多表单信息

Javascript 使用mongodb提交后插入更多表单信息,javascript,mongodb,Javascript,Mongodb,我有一个简单的表格: <form action="/persons" method="POST"> Name:<input type="text" name="name"><br> URL:<input type="text" name="quote"><br> <button type="submit">Submit</button> </form> 现在,对于

我有一个简单的表格:

  <form action="/persons" method="POST">
    Name:<input type="text"  name="name"><br>
    URL:<input type="text"  name="quote"><br>
    <button type="submit">Submit</button>
  </form>
现在,对于每个url和名称,我希望文档包含更多的值(用户不插入),比如ratio,在用户发送“submit”后应该插入到db中

例如,给定某个url和名称“Jessica Alba”,我希望文档包含: {url:..,名称:Jessica Alba,比率:0}


我尝试了不同的事情,但没有成功。最好的选择是什么?

可以使用点运算符访问Json对象的属性

节点文件:

var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/test',connectCallback);
function connectCallback(err, db) {
  col = db.collection('persons');
  console.log('Connected to mongodb');
  }

var app = express();
//middlewares
app.use(bodyParser());
//routes
app.get('/', function (req, res) {
  res.sendFile(__dirname+'/index.html');
});
app.post('/persons', (req, res) => {
    req.body.ratio=0
    col.save( req.body, (err, result) => {
        if (err) 
            return console.log(err)
        console.log(result)
        console.log('saved to database')
        console.log(req.body)
        res.redirect('/')
    })
})
setTimeout(function() {
    app.listen(3000, function () {
    console.log('Sample app listening on port 3000!');
    });
}, 1000);
Index.html文件:

<html>
    <body>
        <form action="/persons" method="POST">
            Name:<input type="text"  name="name"><br>
            URL:<input type="text"  name="quote"><br>
            <button type="submit">Submit</button>
        </form>
    </body>
</html>

名称:
网址:
提交
在保存它之前,你是否尝试添加req.body.ratio=0?你的意思是代替req,body,{req.body,req.body.ratio=0}?我的意思是在调用db.collectionName.save()之前,添加一行来添加ratio属性.db.collection('persons')。save({ratio:“0”,年龄:19,状态:“P”}),当我之前添加它时,它不起作用…app post('/persons',(req,res=>{req.body.ratio=0 db.collection('persons').save(req.body,(err,result)。。。。。
<html>
    <body>
        <form action="/persons" method="POST">
            Name:<input type="text"  name="name"><br>
            URL:<input type="text"  name="quote"><br>
            <button type="submit">Submit</button>
        </form>
    </body>
</html>