Azure Mobile Services, Custom API, update ouput followed by select returns empty results

倖福魔咒の 提交于 2019-12-11 01:35:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!