2017-05-07 108 views
0

我正在测试一些NodeJS设置API的测试。UnhandledPromiseRejectionWarning:未处理的承诺拒绝NodeJS MSSQL

但是,当SQL返回有关NULL列的错误时,我的http调用会挂起,您可以在节点控制台中看到错误。

我得到的错误是

node:7397) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): RequestError: Cannot insert the value NULL into column 'FeatureEntryId', table 'testdb STING.dbo.J_ProductFeaturesRelation'; column does not allow nulls. INSERT fails."

在了解SQL错误,这很好我只是想给的NodeJS返回此错误信息给客户端。

这是代码。

//Add or update feature relations 
var insertFeatureRelation = (callback, productid, featureid) => { 
    console.log(productid + ' ' + featureid) 
    var conn = new sql.Connection(settings.dbConfig()) 
    conn.connect().then(function (conn) { 
     var request = new sql.Request(conn); 
     request.input('productid', sql.VarChar, productid); 
     request.input('featureid', sql.VarChar, featureid); 
     request.execute('PM_InsertFeatureRelation').then(function (recordsets, returnValue, affected) { 
      callback(recordsets) 
     }) 
    }).catch(function (err) { 
     console.log('ffs'); 
     callback(null, err); 
    }); 
} 
exports.insertFeatureRelation = function (req, resp, productid, featureid) { 
    insertFeatureRelation(function (data, err) { 
     if (err) { 
      httpMsgs.show500(req, resp, err) 
     } else { 
      httpMsgs.sendJSON(req, resp, data) 
     } 
     resp.end(); 
    }, productid, featureid) 
}; 

如果存储过程运行正常,但代码工作正常,但错误只是实际上一直到页面。

这里是httpMsgs的内容:

exports.show500 = function(req, resp, err) { 
    console.log('heree') 
     resp.writeHeader(500, {"Content-Type": "application/json"}); 
     resp.write(JSON.stringify({data: "Error:" + err})) 
}; 

exports.sendJSON = function(req, resp, data) { 
    if (data){ 
     resp.writeHeader(200, {"Content-Type": "application/json"}); 
     resp.write(JSON.stringify(data)); 
    } 
} 
+0

你的问题是什么? – MrJLP

+0

@MrJLP为什么httpMsgs.show500(req,resp,err)在SQL返回错误时不会触发。我会认为它会在catch中发现错误,然后运行console.log('ffs')和回调函数。 – Adam91Holt

+0

失败的承诺是'request.execute('PM_InsertFeatureRelation')',你没有抓住它。 – naortor

回答

0

以供将来参考......之所以节点挂是因为从SQL错误的执行功能的渔获物没有抓到。

这里是更新的代码。

//Add or update feature relations 
var insertFeatureRelation = (callback, productid, featureid) => { 
    console.log(productid + ' ' + featureid) 
    var conn = new sql.Connection(settings.dbConfig()) 
    conn.connect().then(function (conn) { 
     var request = new sql.Request(conn); 
     request.input('productid', sql.VarChar, productid); 
     request.input('featureid', sql.VarChar, featureid); 
     request.execute('PM_InsertFeatureRelation').then(function (recordsets, returnValue, affected) { 
      callback(recordsets) 
     }).catch(function (err) { 
      console.log(err); 
      callback(null, err); 
     }) 
    }).catch(function (err) { 
     console.log(err); 
     callback(null, err); 
    }); 
} 
exports.insertFeatureRelation = function (req, resp, productid, featureid) { 
    insertFeatureRelation(function (data, err) { 
     if (err) { 
      httpMsgs.show500(req, resp, err) 
     } else { 
      httpMsgs.sendJSON(req, resp, data) 
     } 
     resp.end(); 
    }, productid, featureid) 
}; 
0

虽然@ Adam91Holt的答案是确定的,它可以进一步简化为

var insertFeatureRelation = (callback, productid, featureid) => { 
    console.log(productid + ' ' + featureid) 
    var conn = new sql.Connection(settings.dbConfig()) 
    conn.connect().then(function (conn) { 
     var request = new sql.Request(conn); 
     request.input('productid', sql.VarChar, productid); 
     request.input('featureid', sql.VarChar, featureid); 
     return request.execute('PM_InsertFeatureRelation'); 
    }).then(function (recordsets) { 
     callback(recordsets); 
    }).catch(function (err) { 
     console.log(err); 
     callback(null, err); 
    }); 
} 

的好处是,有错误处理

说实话,虽然单点,我不会” t使用回调范式与承诺,除非我必须

var insertFeatureRelation = (productid, featureid) => { 
    console.log(productid + ' ' + featureid) 
    var conn = new sql.Connection(settings.dbConfig()) 
    conn.connect().then(function (conn) { 
     var request = new sql.Request(conn); 
     request.input('productid', sql.VarChar, productid); 
     request.input('featureid', sql.VarChar, featureid); 
     return request.execute('PM_InsertFeatureRelation'); 
    }); 
} 
exports.insertFeatureRelation = function (req, resp, productid, featureid) { 
    insertFeatureRelation(productid, featureid).then(function(data) { 
     httpMsgs.sendJSON(req, resp, data) 
     resp.end(); 
    }).catch(function (err) { 
     httpMsgs.show500(req, resp, err) 
     resp.end(); 
    }); 
}; 
相关问题