问题
The following results.length is always 0 while the row is defintely updated.
exports.get=function(request,response){
var mssql=request.service.mssql;
var sql="declare @tableId table(id nvarchar(255)); update mytable set myfield=1 output inserted.id into @tableId where id=(select top 1 id from mytable where myfield is null order by date); select id from @tableId;";
mssql.query(sql, {
success:function(results){
if(results.lentgh==1)
response.send(statusCodes.OK, results[0]);
else
response.send(statusCodes.OK,null);
}
});
});
回答1:
Your SQL command passed to the mssql.query
function has two statements: the update, and the select. Each of those statements will generate a "result" (a call to the success callback). Since you're sending a response when you get the first result, and it's the result for the update call (which doesn't "return" anything), your API is always going to return an empty response, even if something was updated.
Another problem is that you have a typo in getting the length of the results: the correct is length
, while in your code you have lentgh
. As this is JavaScript, this will be interpreted as undefined (0) and the "if" clause will never be executed.
Try replacing the code with the code below, and you should start seeing the response when an update is made.
exports.get = function(request, response) {
var mssql=request.service.mssql;
var sql="declare @tableId table(id nvarchar(255)); update mytable set myfield=1 output inserted.id into @tableId where id=(select top 1 id from mytable where myfield is null order by date); select id from @tableId;";
var firstResponse = true;
mssql.query(sql, {
success : function(results) {
if (firstResponse) {
// result from the update call, can ignore
firstResponse = false;
} else {
if(results.length == 1) {
response.send(statusCodes.OK, results[0]);
} else {
response.send(statusCodes.OK,null);
}
}
}
});
};
来源:https://stackoverflow.com/questions/27531734/azure-mobile-services-custom-api-update-ouput-followed-by-select-returns-empty