0

I'm trying to make an external module outside my index.js to make the code more manageable. However, it does not return what I wanted.

Here's my code in index.js

app.post('/resumes', authenticate, (req, res) => {

    var body = _.pick(req.body, ['url']);
    var url = body.url;
    request(url)
    .then(function(html){
        //success!
        html = html.replace(/<(?:.|\n)*?>/gm, '');
        html = html.replace(/\t/g, '');
        html = html.replace(/\n/g, '');

        scrape.searchJob(req.user._id, html).then(jobs => {
            res.status(200).send(jobs);
        });
    })
    .catch(function(err){
        res.status(400).send({err});
    });
});

Here's the scrape.js:

var request = require('request-promise');
var {Job} = require('../models/job');

var searchJob = ( id, html) => {
    var promise = Job.find({_creator: id}).exec(function (err, jobs){
        jobs.map((job) => {
            job.butter = 'butter';
        });
    });
return promise;
};

module.exports = {searchJob};

I'm trying to do some modification the jobs that I got from Job (mongoose model), yet it still only returns the original data.

I did require scrape.js in index.js.

Any help appreciated!

Kei
  • 611
  • 2
  • 11
  • 24
  • 1
    Quite a few more problems in the code than just the linked duplicate, but it's a start. `Job.find({ ... }).then(jobs => { return jobs })` is basically wrong. As is `console.log('B: ', scrape.searchJob(req.user._id, html))` You don't return values from Promises like that. Even `var {Job} = require('../models/job')` looks suspect here and is more than likely meant to be `var Job = require('../models/job')` unless for some reason you are doing `module.exports.Job = mongoose.model('Job', ...)` elsewhere. But pay attention to working with Promise resolving first. – Neil Lunn Nov 13 '18 at 01:15
  • @NeilLunn Thanks for the comment and the link to the previous question. I'll read the other post to see how I should adjust this! – Kei Nov 13 '18 at 01:24

0 Answers0