using express to display events and gatherings from external websites

I've created a simple Express application with a single function that utilizes the nodejs request and selects specific div elements. My goal is to then render this using jade.

var express = require('express');
var voc = require('vocabulaire');
var async = require('async');

var router = express.Router();

router.get('/', function (req, res) {
    res.render('index', {title: 'Espace de la diffusion'});
});

var result;
router.get('/search/:mot', function (req, res) {
    async.series([
        function () {
            result = main(['conj', req.params.mot]);
            console.log('in 1');
        },
        function () {

            res.render('index', {title: 'Espace de la diffusion', data: result});
            res.send(html);
            console.log('in 2');
        },
    ]);    
});    

module.exports = router;


var request = require('request')
    , cheerio = require('cheerio');

function doit(verbe, result) {
    var url = 'http://www.babla.ru/%D1%81%D0%BF%D1%80%D1%8F%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F/%D1%84%D1%80%D0%B0%D0%BD%D1%86%D1%83%D0%B7%D0%BA%D0%B8%D0%B9/' + verbe;

    request(url, function (err, resp, body) {
        $ = cheerio.load(body);
        var temps = $('.span4.result-left h5');
        if (temps.length == 0) {
            console.log('results not found');
        }
        else {
            console.log('result found');
            debugger;
            return $('.span4.result-left');
        }
    });
}


function main(arg) {
    switch (arg[0]) {
        case 'conj':
            return doit(arg[1]);
            break;
        default:
            console.log('unknown parameter');
            break;
    }
}

I decided to use the async library to ensure that my result is prepared for rendering. However, in the console, I am seeing the following:

GET /search/est - - ms - -
in 1
result found

then the debugger guiding me to the nodejs function makeTick()... I'm at a loss on what to do next. Can someone please assist me?

Answer №1

The async.series() functions you provided are lacking the necessary callback parameter to allow the next function to execute. Interestingly, you can achieve a single async task without utilizing async:

main(['conj', req.params.mot], function(err, result) {
  res.render('index', {title: 'Espace de la diffusion', err: err, data: result});
});

// ...

function doit(verbe, result, callback) {
    var url = 'http://www.babla.ru/%D1%81%D0%BF%D1%80%D1%8F%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F/%D1%84%D1%80%D0%B0%D0%BD%D1%86%D1%83%D0%B7%D1%81%D0%BA%D0%B8%D0%B9/' + verbe;

    request(url, function (err, resp, body) {
        if (err)
          return callback && callback(err);

        $ = cheerio.load(body);
        var temps = $('.span4.result-left h5');
        if (temps.length == 0) {
            callback && callback();
        }
        else {
            callback && callback(null, $('.span4.result-left'));
        }
    });
}


function main(arg, callback) {
    switch (arg[0]) {
        case 'conj':
            doit(arg[1], callback);
            break;
        default:
            callback && callback(new Error('unknown parameter'));
            break;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sequelize's findOne method has returned an instance, but the row data is stored in the dataValues property. However, the direct

Background: I am in the process of testing a basic model going into and coming out of the database. This is not an actual test, but rather a prelude to some integration tests. The tools being used include Sequelize and findOne. The issue: When directly ac ...

Are API tokens securely stored within a Flux (Redux) store environment?

Is it considered secure to store an API token retrieved from an authentication call in a Flux store (specifically, Redux)? I have utilized Webpack to bundle all assets in the project, so I believe the store is isolated and inaccessible to external scripts ...

How does the method of including JavaScript libraries in HTML differ from adding them as npm dependencies?

Upon browsing through npm highly regarded packages, I noticed that popular projects such as Grunt, lodash, and underscore are readily available. I have always utilized these in the traditional manner: <script src="js/lib/lodash.min.js"></script& ...

What sets apart importing crypto from 'crypto' as opposed to simply using crypto in Node.js?

For my upcoming Node project, I'm considering using crypto to obtain a UUID. After some research, I discovered that I can achieve this by utilizing crypto and that it is already a declared variable without the need for importing it. So there are two o ...

Prevent unauthorized entry to nodejs express endpoints

I am facing a challenge with the "internal" routes within my application: app.get('/myroute', function(req, res) { res.status(200).send('Hello!'); }); The issue is that this route can also be accessed from external sources: curl http ...

Is there a way to identify a location in close proximity to another location?

With a position at (9,-3), I am looking to display all positions surrounding it within a square red boundary. However, I am struggling to find the algorithm to accomplish this task. Any help or alternative solutions would be greatly appreciated. Thank you ...

An unusual Error instance is appearing in the console logs

Attempting to submit comments using the YouTube API, but encountering errors during the process. Upon logging the error object, the following is displayed: { [Error: Bad Request] code: 400 } The format appears strange at first glance (key-value inside a ...

combining arrays from two collections in MongoDB

In my database, I have two collections: Clinics and Doctors. Clinics can have multiple Doctors. The relationship between them was established as follows: doctor: [{ doctorId: { type: mongoose.Schema.Types.ObjectId, ref: 'doctors&apos ...

Leverage the power of Stripe Webhooks in nodejs to automatically retrieve updated information and seamlessly update the database

I am a novice when it comes to using stripe webhooks. My application is based on user subscriptions, and I have successfully enabled users to activate their monthly subscription plan using Stripe. Now, my goal is to automatically update my database with th ...

Is it advisable to implement the modular pattern when creating a Node.js module?

These days, it's quite common to utilize the modular pattern when coding in JavaScript for web development. However, I've noticed that nodejs modules distributed on npm often do not follow this approach. Is there a specific reason why nodejs diff ...

Having trouble with my Express.js logout route not redirecting, how can I troubleshoot and resolve it?

The issue with the logout route not working persists even when attempting to use another route, as it fails to render or redirect to that specific route. However, the console.log("am clicked"); function works perfectly fine. const express = require('e ...

How can you identify pending database migrations in your development environment using Prisma?

Challenge Currently, I am immersed in developing a T3 stack application consisting of Next.JS, Prisma ORM, Typescript, and more. Upon receiving updates from my colleagues, it's common to encounter database migrations within those changes. The issue ...

Discovering the source of an error in Jest: Unveiling the stack trace and cause

I am currently troubleshooting a nodeJS application. I encountered an error where a variable is undefined. When running the code without Jest, the error was clear and easily located: without jest: ➜ server git:(dc/build) ✗ node test/runner.js /Users/ ...

Node.js SQLite3 - DB.each() not running the subsequent code block

When running the code snippet below, I am getting the following output: var db = new sqlite3.Database("database.sqlite") console.log("2") db.each("SELECT * FROM gban WHERE id = '"+id+"'", async functi ...

There seems to be an issue with node.js - headers cannot be set after they have already been sent to

As I work on developing my blog, I've encountered an issue with rendering different paths using the router parameter. Each time I attempt to display another route, an error surfaces. Unfortunately, I'm unable to provide more details at this momen ...

What steps should be taken to resolve an 'ERR_REQUIRE_ESM' issue?

I'm currently experimenting with the chalk npm package. Here is my code snippet: const chalk = require('chalk'); console.log( chalk.green('All sytems go') + chalk.orange('until').underl ...

Executing a Node.js script using an absolute path

In the process of developing a basic app using Node.js and Express, I've encountered an issue. The script is located at path/to/script/server.js. When I run this script with node server.js while in the correct directory, everything functions correctly ...

Encountering an error with npm installation when building dependencies

Currently, I am executing npm install on the package.json project file provided below: { "name": "tradesync", "version": "1.0.0", "description": "", "main": "consumer.js", "dependencies": { "async": "^2.6.1", "avsc": "^5.1.1", "cron" ...

Removing various data entries based on timestamp and additional fields in Firebase Firestore

I stumbled upon a similar query before. Being new to coding, I am attempting to remove all documents in the "users" collection that are older than 1 month and not marked as premium. The deletion criteria involve checking if the "user_online" field is at le ...

The Unavailability of Environment Variables in Node.js Debugger in VSCode

When I use the debugger in Visual Studio Code, an error message appears stating: MongooseError: The `uri` parameter for `openUri()` must be a string, but it is showing as "undefined". Ensure that the first parameter for `mongoose.connect()` or `mongoose.cr ...