最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose save all parameters from request body - Stack Overflow

programmeradmin0浏览0评论

I'm setting up a little API, I do some data validation on the client side of my application and whilst I do that I'm structuring my data to match my mongoose schema.

I'm attempting to do this...

router.route('/car')
.post(function(req, res) {
    var car = new Car();

    car = req.body; // line under scrutiny

    car.save(function(err) {
        if(err) {
            console.log(err);
            res.status(400);
            res.send(err);
        }
        else {
            res.status(200);
            res.json({
                message: req.body.name + ' successfully registered!'
            });
        }
    });
});

but of course, this is currently removing all the model parameters of car provided by mongoose, so the save method e.t.c are no longer functional.

I have attempted car.data = req.body but this requires all of my mongoose schemas to be wrapped into a data object which isn't so elegant.

I was wondering if there was any way to avoid preparing the car object to be saved without the long-hand of;

car.name = req.body.name;
car.seats = req.body.seats;
car.engine_size = req.body.engine_size; 

e.t.c.

I'm essentially wanting to do the equivalent of car.push(req.body); but again, the .push() method is not available to mongoose models.

I'm setting up a little API, I do some data validation on the client side of my application and whilst I do that I'm structuring my data to match my mongoose schema.

I'm attempting to do this...

router.route('/car')
.post(function(req, res) {
    var car = new Car();

    car = req.body; // line under scrutiny

    car.save(function(err) {
        if(err) {
            console.log(err);
            res.status(400);
            res.send(err);
        }
        else {
            res.status(200);
            res.json({
                message: req.body.name + ' successfully registered!'
            });
        }
    });
});

but of course, this is currently removing all the model parameters of car provided by mongoose, so the save method e.t.c are no longer functional.

I have attempted car.data = req.body but this requires all of my mongoose schemas to be wrapped into a data object which isn't so elegant.

I was wondering if there was any way to avoid preparing the car object to be saved without the long-hand of;

car.name = req.body.name;
car.seats = req.body.seats;
car.engine_size = req.body.engine_size; 

e.t.c.

I'm essentially wanting to do the equivalent of car.push(req.body); but again, the .push() method is not available to mongoose models.

Share Improve this question asked Dec 5, 2014 at 12:25 zupcfevfzupcfevf 1031 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

You can pass the req.body to your Car like this

var car = new Car(req.body);

Here's also a reference: Passing model parameters into a mongoose model

发布评论

评论列表(0)

  1. 暂无评论