无法使用mlab-mongodb向远程数据库发出发布请求?

时间:2018-08-17 05:28:12

标签: node.js mongodb express mongoose mlab

我正在尝试使用postman-chorme扩展名向我的数据库发出发布请求  网址为http://localhost:5000/api/users/register

在发出发布请求之前,它可以完美运行并且不会产生错误,但是在发布请求之后,它将引发错误,

gd@gd10:~/Desktop/find-geeks$ npm run server
> find-geeks@1.0.0 server /home/gd/Desktop/find-geeks
> nodemon server.js
[nodemon] 1.18.3[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*[nodemon] starting `node server.js`
Server is running at port no 5000MongoDB connectedTypeError: Cannot read property 'email' of undefined
    at router.post (/home/gd/Desktop/find-geeks/routes/api/users.js:22:34)
    at Layer.handle [as handle_request] (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/layer.js:95
:5)    at next (/home/gd/Desktop/find-geeks/node_modules/ex
press/lib/router/route.js:137:13)    at Route.dispatch (/home/gd/Desktop/find-geeks/node_
modules/express/lib/router/route.js:112:3)    at Layer.handle [as handle_request] (/home/gd/Deskto
p/find-geeks/node_modules/express/lib/router/layer.js:95
:5)
    at /home/gd/Desktop/find-geeks/node_modules/express/
lib/router/index.js:281:22
    at Function.process_params (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:335:12)
    at next (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:174:3)
    at router (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:317:13)
    at /home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:335:12)
    at next (/home/gd/Desktop/find-geeks/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/home/gd/Desktop/find-geeks/node_modules/express/lib/middleware/init.js:40:5)

我能够连接远程数据库,但是无法发布请求。 users.js文件引发错误,

router.post("/register", (req, res) => {
  User.findOne({ email: req.body.email }).then(user => {
    if (user) {
      return res.status(400).json({ email: "Email value exists already." });
    } else {
      console.log("user not found");
      const avator = gravator.url(req.body.email, {
        s: "200", //Size of gravator in pixels
        r: "pg", //rating,
        d: "mm" //default value= 'mm'
      });
      // create user
      const newUser = new User({
        name: req.body.name,
        email: req.body.email,
        avator,
        password: req.body.password
      });

我能够在mlab上连接远程数据库

gd@gd10:~/Desktop/find-geeks$ mongo ds225382.mlab.com:25382/findgeeks -u gdgeek -p gd@123
MongoDB shell version v3.6.6
connecting to: mongodb://ds225382.mlab.com:25382/findgeeks
MongoDB server version: 3.6.6
rs-ds225382:PRIMARY> show dbs
2018-08-17T10:21:06.243+0530 E QUERY    [thread1] Error: listDatabases failed:{
        "operationTime" : Timestamp(1534481464, 1),
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $clusterTime: { clusterTime: Timestamp(1534481454, 1), signature: { hash: BinData(0, E3909ED95273847820FEF45BA1D664D60F796DB2), keyId: 6590452867025338369 } }, $db: \"admin\" }",
        "code" : 13,
        "codeName" : "Unauthorized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1534481464, 1),
                "signature" : {
                        "hash" : BinData(0,"cF9xY92R3CPeenwCnvqzuNBhtkw="),
                        "keyId" : NumberLong("6590452867025338369")
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:849:19
shellHelper@src/mongo/shell/utils.js:739:15
@(shellhelp2):1:1
rs-ds225382:PRIMARY>

我的完整代码在这里github

1 个答案:

答案 0 :(得分:1)

要使用req.body,您需要使用身体解析器。

要添加正文解析器(在您的项目目录中):

npm install --save body-parser

然后在您的user.js中,添加以下行(在顶部):

const bodyParser = require("body-parser");
router.use(bodyParser.json());