Problem with recovering the return of a Promisse

Asked

Viewed 33 times

0

Before I was with a problem that when there was some error in the execution of the query the server stopped and had to restart to get back to work, so giving a search I found how to solve the problem by dealing with the errors with the methods then and cacth.

But before using then and cacth, in my model when there was no error it would return to the controller normally the id of the item inserted in the database.

Model code before use then and cacth:

const db = require('../database/connection');
module.exports = {
    create: async ({ title, description, banner, vacancies, type }) => {

    return new Promise((resolver, reject) => {
        db.query('INSERT INTO events (`title`, `description`,  `banner`, `vacancies`, `type`) VALUES ( ?,?,?,?,?)',
            [title, description, banner, vacancies, type],
            (error, results) => {
                if (error) { reject(error); return; }
                resolver(results.insertId);
            }
        )
    }); 
}

}

After using then and cacth the server behaved as expected when an error occurred, showing the error but did not stop working, only that the problem arose that in my controller when calling the insertion function Let Event = await Event.create(values); in my Event variable should receive the id of the inserted item, so it is running before finishing the insertion process.

Model code with the use of then and cacth:

const db = require('../database/connection');
module.exports = {
create:async ({ title, description, banner, vacancies, type }) => {

    var res =  new Promise((resolver, reject) => {
        db.query('INSERT INTO events (`title`, `description`,  `banner`, `vacancies`, `type`) VALUES ( ?,?,?,?,?)',
            [title, description, banner, vacancies, type],
            (error, results) => {

                if (error) { reject(error); } 
                resolver(results);
            }
        )
    });

    res.then(insertId => {
        console.log(insertId, 'event');
        return(insertId);
        
    }).catch(rejectValeu => {
        console.log({rejectValeu});
        return;
    });

}

}

controller code:

const Event = require('../models/Event');

module.Exports = { Create: async (req, res) => {

    let json = { error: {}, result: {}, status: {} };

    let { title, description, banner, vacancies, type } = req.body;

    let values = {
        title: title,
        description: description,
        banner: banner,
        vacancies: vacancies,
        type: type
    }

    let event = await Event.create(values);
    console.log(event, `controller`);

    if (event) {
        json.result = {
            id: event
        }
    }
    
    res.json(json);
 }}

I was told to use Try/catch on the model, but I was unsuccessful.

1 answer

0

Try :

create:async ({ title, description, banner, vacancies, type }) => {
 
 var insert = () => {
        var res =  new Promise((resolver, reject) => {
    db.query('INSERT INTO events (`title`, `description`,  `banner`, `vacancies`, `type`) VALUES ( ?,?,?,?,?)',
        [title, description, banner, vacancies, type],
        (error, results) => {

            if (error) { reject(error); } 
            resolver(results);
        }
    )
});
return res;
};

try{
   var insertId = await insert();
   return(insertId);
}
catch(e)
{
      console.log({rejectValeu});
    return;
} }
  • I had put to return my Promisse direct: Return new Promisse (...); and in my controller I used then/catch this way: var Event = await Event.create(values). then(insertId => { Return(insertId); }). (rejectValeu => { console.log({rejectValeu}); Return; }); , and it worked, but I’ll test it the way you said, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.