我不熟悉后端代码。
我正在尝试将基本行插入MongoDB在线数据库。
这些是我的文件:
server.js:
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const app = express();
var db = require('./config/db');
const port = 8000;
app.use(bodyParser.urlencoded({ extended: true }));
MongoClient.connect(db.url, (err, database) => {
if (err) return console.log(err);
db = database.db('note-api');
require('./app/routes')(app, db);
require('./app/routes')(app, database);
app.listen(port, () => {
console.log('We are live on ' + port);
});
})
note_routes.js:
module.exports = function (app, db) {
// const collection =
app.post('/notes', (req, res) => {
const note = { text: req.body.body, title: req.body.title };
db.collection('notes').insert(note, (err, result) => {
if (err) {
res.send({ 'error': err });
} else {
res.send(result.ops[0]);
}
});
});
};
db.js:
module.exports = {
url: "mongodb://laelav:laelav1@ds227594.mlab.com:27594/getremp"
};
每当我尝试使用POST并希望更新在线数据库时-我会收到未授权的错误: unauthorized error
然后我在note_routes.js中添加以下行:
db.grantRolesToUser("laelav", [{ role: "readWrite", db: "getremp" }]);
并得到以下“ TypeError:db.grantRolesToUser不是函数”的信息: not a function error
请帮助!