简体   繁体   中英

promise returns undefined while calling from two different mysql queries

I Have two mysql queries that runs with promise.

The first one is updates information on a mysql table and then resolves the issue and calls the next mysql query. The problem is that, when it calls the next mysql query the promise returns UNDEFINED and I am not sure why. When I console.log it out in my node js server post request, it gives undefined. I documented on the code which areas are problems.

UpdateUserPath = (data) => new Promise((resolve,reject)=>{
    data.UPDATE_DT = getDateTime();
    db.query('UPDATE path  UPDATE_DT = ?  where Owner = ?',
        [data.UPDATE_DT, data.Owner], function(err,results,fields){
        if(err){
            reject('Could not update user path');
        }else{
            if(results.affectedRows > 0){
                data.ID = null;
                data.UPDATE_DT = null;
                // The problem is here, when this gets resolved it calls the other function SaveUserPath
                resolve(saveUserPath(data));
            }else{
                reject('Could not update user path');
            }
        }
    });
});


saveUserPath = (data) => new Promise((resolve, reject) => {
    db.query('INSERT INTO path SET ?', data, function (error, results, fields) {
        if (error) {
            reject('Could not insert path');
        }else{
            var Id = results.insertId;

            db.query('UPDATE path SET ORIG_ID = ? where ID = ?',[Id, Id], function(err,results,fields){
                if(err){
                    reject('Could not insert row to path  table - saveuserpath');
                }else{
                    if(results.affectedRows > 0){
                       // THIS INFORMATION HERE IS UNDEFINED
                        return resolve(results[0]);
                    }else{
                        reject('Could not update path');
                    }
                }
            });
        }
    });
});

In the server it gets called like this.

getUserPath(req.session.userid).then((path_data)=>{
                path_data.status = 1;
                UpdateUserPath(path_data).then((result)=>{
                    console.log(result); // THIS IS UNDEFINED
                });
            });

I am wondering if resolve(saveUserPath(data)); is the right way to call another promise which is not outside in the server. I was thinking of just doing it this way.

UpdateUserPath(path_data).then((result)=>{
   saveUserPath(result).then((result_save) => {
     console.log(result_save); // THIS MIGHT WORK 
   });
});

But why is the normal way wrong.

I have several guesses why it isn't working, but there are a number of things wrong such that it's better to just clean up the code to a much better design.

When combining multiple asynchronous callback-driven operations in an otherwise promise-based interface, you really want to promisify the underlying functions at their lowest level and then you can implement all your control flow and error handling using the benefits of promises. I think that will also make your problem go away and probably fix a couple other bugs too.

// promisify db.query()
// if a promisified interface is built into your database, use that one instead
db.queryP = function(q, d) {
    return new Promise((resolve, reject) {
        db.query(q, d, (err, results, fields) => {
            if (err) {
                reject(err);
            } else {
                resolve(results);
            }
        });
    });
}

UpdateUserPath = function(data) {
    data.UPDATE_DT = getDateTime();
    let q = 'UPDATE path  UPDATE_DT = ?  where Owner = ?';
    return db.queryP(q, [data.UPDATE_DT, data.Owner]).then(results => {
        if (results.affectedRows > 0) {
            data.ID = null;
            data.UPDATE_DT = null;
            return saveUserPath(data);
        } else {
            throw new Error('Could not update user path');
        }
    });
}

saveUserPath = function(data) {
    let q = 'INSERT INTO path SET ?'
    return db.queryP(q, data).then(results => {
        let q2 = 'UPDATE path SET ORIG_ID = ? where ID = ?';
        var Id = results.insertId;
        return db.queryP(q2, [Id, Id]).then(results2 => {
            if (results2.affectedRows > 0) {
                return results2[0];
            } else {
                throw new Error('Could not update path');
            }
        });
    });
}

getUserPath(req.session.userid).then(path_data => {
    path_data.status = 1;
    return UpdateUserPath(path_data);
}).then(result => {
    // process result here
}).catch(err => {
    // process error here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM