操作无法使用REST,但以其他方式工作

时间:2018-04-02 23:51:10

标签: javascript node.js mongodb api express

这有点奇怪,但我不知道造成这种疯狂的原因是什么。

我有这段代码:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var AdModule = require('../models/ad');

function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) {
        return next(); // User is logged in
    }
    res.redirect('/users/login');
}

router.route('/')
    .get((req, res) => {
        AdModule.find((err, col) => {
            if (err)
                console.log(err);
            res.render('ads/', {
                user: req.user,
                ads: col,
                title: "Ads"
            });
        })
    });

router.route('/create')
    .get(isLoggedIn, (req, res) => {
        res.render('ads/create', {
            user: req.user,
            title: "Ads"
        });
    })
    .post(isLoggedIn, (req, res) => {
        var ad = new AdModule();
        ad.title = req.body.title;
        ad.desc = req.body.desc;
        ad.price = req.body.price;
        ad.save(err => {
            if (err)
                console.log(err);
            res.redirect('/ads');
        });
    });

router.route('/:_id')
    .get(isLoggedIn, (req, res) => {
        console.log('tada');
        AdModule.findById(req.params._id, (err, obj) => {
            if (err)
                console.log(err);
            res.render('ads/edit', {
                ad: obj,
                title: 'Edit ad',
                user: req.user
            });
        })
    })
    .post(isLoggedIn, (req, res) => {
        AdModule.findById(req.params._id, (err, obj) => {
            if (err)
                res.send(err);
            obj.title = req.body.title;
            obj.desc = req.body.desc;
            obj.price = req.body.price;
            obj.save(err => {
                if (err)
                    console.log(err);
                res.redirect('/ads');
            });
        })
    });

router.route('/remove/:_id')
    .post(isLoggedIn, (req, res) => {
        AdModule.remove({_id: req.params._id}, (err, obj) => {
            if (err)
                console.log(err);
            res.redirect('/ads');
        })
    });

module.exports = router;

此代码不是RESTful,但有效。数据库正在更新,这一切都很好。

现在我尝试将其作为API运行。代码是相同的,唯一的区别是路线和退货。这些操作是复制粘贴的。

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var AdModule = require('../../models/ad');

router.route('/')
    .get((req, res) => {
        AdModule.find((err, col) => {
            if (err)
                res.send(err);
            res.json(col);
        })
    })
    .post((req, res) => {
        var ad = new AdModule();
        ad.title = req.body.title;
        ad.desc = req.body.desc;
        ad.price = req.body.price;
        ad.save(err => {
            if (err)
                res.send('err' + err);
            res.json({msg: 'Created'});
        });
    });

router.route('/:_id')
    .get((req, res) => {
        AdModule.findById(req.params._id, (err, obj) => {
            if (err)
                res.send(err);
            res.json(obj);
        })
    })
    .put((req, res) => {
        AdModule.findById(req.params._id, (err, obj) => {
            if (err)
                res.send(err);
            console.log(obj);
            console.log(req.params._id);
            obj.title = req.body.title;
            obj.desc = req.body.desc;
            obj.price = req.body.price;
            obj.save(err => {
                if (err)
                    res.send(err);
                res.json({msg: 'Updated'});
            });
        })
    })
    .delete((req, res) => {
        AdModule.remove({_id: req.params._id}, (err, obj) => {
            if (err)
                res.send(err);
            res.json({msg: 'Removed'});
        })
    });

module.exports = router;
var a = {
    "desc": "Hi. I'd like to sell shoes",
    "price": 12.28,
    "title": "Selling shoes"
};

如您所见,代码几乎相同。但是当我使用邮递员更新或创建新对象时,它会创建它们,但没有titledescriptionprice字段。所以我猜这个模型有问题(在这里找到)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var AdSchema = new Schema({
    title: String,
    desc: String,
    price: Number,
});

module.exports = mongoose.model('Ad', AdSchema);

但我不知道是什么。路径似乎正确,没有错误,邮递员返回message: "created"message: "updated"

{
    "msg": "Created"
}

这就是GET现在的样子。

{
    "_id": "5ac2bfd4bd478155ac1eba37",
    "title": "b",
    "desc": "b",
    "price": 2,
    "__v": 0
},
{
    "_id": "5ac2c1726995a75700cef8eb",
    "__v": 0
}

知道我的问题在哪里吗?

我的创建表单:

<% include ../partials/header.ejs %>
<form method="post" action="/ads/create">
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" name="title" id="title" placeholder="Enter title">
    </div>
    <div class="form-group">
        <label for="desc">Description</label>
        <input type="text" class="form-control" name="desc" id="desc" placeholder="Enter description">
    </div>
    <div class="form-group">
        <label for="price">Price</label>
        <input type="text" class="form-control" name="price" id="price" placeholder="Enter price">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
<% include ../partials/footer.ejs %>

这就是我传递给Postman的原因

"title":"test",
"desc":"test",
"price":12312,

1 个答案:

答案 0 :(得分:0)

确定。事实证明我需要使用postman发送它:

desc:Hi. I'd like to sell shoes again
price:12.28
title:Selling shoes