edencorbin
edencorbin

Reputation: 2949

node.js sails.js custom create beginner

I'm working with sails.js and mongo.db following some tutorials and creating a custom application and things are going well. Largely I'm using the built in, backbone I believe, scrud functions, I'm wondering how I could create a database entry from scratch. For example, the following works great from form data in my Student Controller:

    create: function (req, res, next) {
        Student.create(req.params.all(), function studentCreated(err, student) { 
            if (err) {
                console.log(err);
                req.session.flash = {
                    err: err
                }
                return res.redirect('/');
            }
            res.redirect('/');
        });
    },

For simplicity my Student model currently just has a first name and a one way association with a schools model.

module.exports = {
    attributes: {
        school: {
            model: 'school'
        },
        first_name: {
            type: 'string'
        },
    },
};

Say I wanted to, for the sake of understanding, just create a student with a fixed first name "Bob" and fixed school Id "xyz" in another action, without using the built in backbone create functions, nor using defaultTo in the model, nor using any form data. I would like to just code creating a database entry in an action, for example a test action in my Student controller. How would I go about this? I tried googling this a little, but given it's beginner nature I'm sure my query parameters were not particularly good.

Upvotes: 2

Views: 59

Answers (1)

Alexis N-o
Alexis N-o

Reputation: 3993

If I correctly understand the question, I think you've already done most of the work. In the Student.create() call, replace req.params.all() by {first_name:'Bob', school:'xyz'}.

If you want to mix it with values from the request params, you could use {first_name:'bob', school: req.param('school')}

Upvotes: 2

Related Questions