score:2

Accepted answer

Obviously you can't have two different this value.

The simplest solution would be to capture the outer context in a closure

const component = this

this.db.run(
    'INSERT INTO table...',
    {},
    function (error) {
        console.log(this.lastId) // is available
        // check error
        if(error !== null) {
            component.props.addNotification({
                open: true,
                variant: 'error',
                message: error.message,
                autoClose: null,
            });
        } else {
            component.props.route.history.push('/...');
            component.props.closeForm();
        }
    }
);

or you could create a temp normal function that would pass this.lastId to your callback as the second argument

this.db.run(
    'INSERT INTO table...',
    {},
    (cb => function(){return cb(error, this.lastId)})((error, lastId) => {
        console.log(lastId) // is available
        // check error
        if(error !== null) {
            this.props.addNotification({
                open: true,
                variant: 'error',
                message: error.message,
                autoClose: null,
            });
        } else {
            this.props.route.history.push('/...');
            this.props.closeForm();
        }
    })
)

score:0

You can do following:

// above function
const self= this;
// inside function
function(error) {
  this.lastId;
  self.props.history;
  self.state;
}

Related Query

More Query from same tag