2017-05-07 23 views
0

NodeJSをテストしてAPIをテストすることについての演劇があります。UnhandledPromiseRejectionWarning:未処理の約束の拒否NodeJS MSSQL

しかし、SQLからNULLカラムに関するエラーが返されると、httpコールがハングするだけで、ノードコンソールにエラーが表示されます。私はちょうどNodeJSは、クライアントにこのエラーメッセージを返すようにしたい

私が手にエラーが

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エラーを理解し、それは大丈夫です。

ここにコードがあります。ストアドプロシージャは、エラーがちょうど決して実際にページに通じサムスが細かい実行されている場合

//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の内容です:SQLのエラーは、実行機能のキャッチに巻き込まれることはなかったので、今後の参考のために

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 SQLがエラーを返した場合、httpMsgs.show500(req、resp、err)が起動しないのはなぜですか?私はそれがcatchのエラーを捕らえて、console.log( 'ffs')とコールバックを実行すると思ったでしょう。 – Adam91Holt

+0

あなたがそれを捕まえていない 'request.execute( 'PM_InsertFeatureRelation')'は失敗するという約束です。 – naortor

答えて

0

...理由ノードがハングしました。

ここに更新コードがあります。 @ Adam91Holtの答えはOKですが

//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

、それがさらに

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); 
    }); 
} 

給付ように簡略化することができる

はしかし正直に言うと、エラー処理の単一点があり、私は「wouldnであるということです私がしなければ約束のコールバックパラダイムを使用しないでください

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(); 
    }); 
}; 
関連する問題