T
T
triadecom2019-05-05 18:37:58
MySQL
triadecom, 2019-05-05 18:37:58

Why is a function in node executed multiple times?

Good afternoon!
I am writing a node callback service that receives photos in the dialog, takes their link and sends it to the mysql database.
Faced such a problem that with some periodicity, the service sends links to the database several times.
What could be the problem?

Server
const easyvk = require('easyvk');
const express = require('express');
const bodyParser = require('body-parser');
const api = require('vk-easy');
const send = require('./send');

const { PORT, CONFIRMATION, TOKEN } = require('./config');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const proc = require('./messageProceed')

app.get('/renaissance/api', (req, res) => res.send('Hello World!'));
app.post('/renaissance/api', (req, res) => {
    const { body } = req;



switch (body.type) {
    
    case 'confirmation':
        res.end(CONFIRMATION);
        break;

    default:
        return proc(body.object);
        break;


}

});
Message processing
const send = require('./send.js');
const photoAPI = require('./actions/photo.js');

module.exports = async function( {from_id: userId, text, attachments: files, payload} ) {

    if (files != null) {

        for (i = 0; i < files.length; i++) {
            //send(userId, 'Getting object...');
                if (files[i].type == 'photo') {
                    photoAPI(files[i]);
            }
        }
    }

    return;

};
Sending to db
const easyVK = require('easyvk');
const send = require('../send');
const api = require('vk-easy');
const mysql = require('mysql');

module.exports = function ({ photo: item }) {

    // на стиль решения не обращайте внимания, пока что я просто тестирую эти методы

if (item.photo_2560 != null) return client.query('INSERT INTO testtab (test, field, number) VALUES ("node",\"'+ item.photo_2560 +'\", 322)');

}
}
The database itself, there should be one link, not several, since only one message was sent
5ccf032f67db3445927904.png

I threw off an example above, when sending one single message, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question