node-mssql insert返回未定义的记录集

时间:2016-01-05 15:47:17

标签: javascript sql-server node.js node-mssql

Select语句工作正常,但每当我尝试插入或更新记录集时,受影响的值都是未定义的。插入/更新在DB中工作,我只是无法读取返回的值。

var sql = require('mssql');
var config = {...};

sql.connect(config).then(function() {
  new sql.Request().query("INSERT INTO MyTable (Name, Age) VALUES ('John', 30)").then(function(recordset, affected) {
    console.log('Recordset: ' + recordset);
    console.log('Affected: ' + affected);
  }).catch(function(err) {
    console.log('Request error: ' + err);
  });
}).catch(function(err) {
  if (err) {
    console.log('SQL Connection Error: ' + err);
  }
});

控制台的输出是:

Recordset: undefined
Affected: undefined

我觉得我必须在这里找到一些非常简单的东西。

2 个答案:

答案 0 :(得分:3)

如评论中所述, DataTable dt = new DataTable("Trend"); dt.Columns.Add("Period", typeof(string)); dt.Columns.Add("Points", typeof(int)); DataRow row = dt.NewRow(); row[0] = "4Q'14"; row[1] = 27; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "1Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "2Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "3Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "4Q'15"; row[1] = 200; dt.Rows.Add(row); WorkbookDesigner wd = new WorkbookDesigner(); //Create a designer workbook Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[0]; worksheet.Cells["A1"].PutValue("Period"); worksheet.Cells["A2"].PutValue("&=Trend.Period"); worksheet.Cells["B1"].PutValue("Points"); worksheet.Cells["B2"].PutValue("&=Trend.Points"); Range range1 = worksheet.Cells.CreateRange("A2:A2"); range1.Name = "RANGE1"; Range range2 = worksheet.Cells.CreateRange("B2:B2"); range2.Name = "RANGE2"; wd.Workbook = workbook; wd.SetDataSource(dt); wd.Process(); //Create chart int chartIndex = worksheet.Charts.Add(ChartType.Column, 5, 2, 29, 10); Chart chart = worksheet.Charts[chartIndex]; //Obtain the updated ranges after processing smart markers var r1 = workbook.Worksheets.GetRangeByName("RANGE1"); var r2 = workbook.Worksheets.GetRangeByName("RANGE2"); MessageBox.Show(r2.RefersTo); //Add the nseries collection to a chart chart.NSeries.Add(r2.RefersTo, true); //Get or set the range of category axis values chart.NSeries.CategoryData = r1.RefersTo; chart.NSeries.IsColorVaried = true; wd.Workbook.Save("e:\\test2\\out1chart1.xlsx"); 语句不会返回记录集,因此INSERT未定义。请参阅文档的this section以了解有关如何获取受影响行数的详细信息。

您的代码存在的问题是您希望recordset作为承诺的第二个参数,但承诺只支持一个参数。因此,您必须以这种方式访问​​受影响的行数:

affected

答案 1 :(得分:0)

如果要让id作为输出参数

[HttpPost]
public ActionResult PostImage([FromForm] IFormFile image){
  if (image == null || image.Length == 0)
  {
     return BadRequest();
  }
  // Do something with image
}