1
\$\begingroup\$

I am very new to Node.js and Express and have been doing a "learn as I go" scenario by building a Craigslist-style app where people can bid on jobs.

I am looking to see if this is the best approach for appending a bid to a job.

var _ = require('underscore');
var request = require('request');
var bcrypt = require('bcrypt');

module.exports = function(app, db, jwt, jwtOptions, passport) {
    app.post('/bids', function(req, res) {
        var body = _.pick(
            req.body,
            'value',
            'hourlyOrFixed',
            'status',
            'title',
            'description',
            'JobForBidId'
        );
        body.BidderId = req.user.id;

        db.jobPosting.findOne({
            where: {
                id: body.JobForBidId
            }
        }).then(function(job) {
            if (job) {
                db.bids.create(body).then(function (bid){
                    res.status(200).json({message:'ok'});
                }).catch(function(err){
                    res.status(500).json({
                        status: 'failure',
                        error: err,
                    });
                });
            } else {
                res.status(404).json({
                    message: 'Job not found'
                });
            }
        }).catch(function(err) {
            res.status(500).json({
                status: 'failure',
                error: err,
            });
        });
    });
});
\$\endgroup\$
  • \$\begingroup\$ does db.jobPosting.findOne() return a job, a jobPosting, or something else? Perhaps it would help to include that method, or at least the return value/type. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Dec 13 '17 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.