Azure移动服务,自定义API,更新输出并随后选择返回空结果 [英] Azure Mobile Services, Custom API, update ouput followed by select returns empty results

查看:52
本文介绍了Azure移动服务,自定义API,更新输出并随后选择返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在明确更新该行的同时,以下result.length始终为0.

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

推荐答案

传递给mssql.query函数的SQL命令具有两个语句:update和select.这些语句中的每一个都会生成一个结果"(对成功回调的调用).由于您是在获得第一个结果时发送响应的,而这是update调用的结果(不会返回"任何内容),因此即使更新了某些内容,您的API也会始终返回空响应.

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.

另一个问题是,您在获取结果的长度时有错字:正确的是length,而在代码中则是lentgh.由于这是JavaScript,因此它将被解释为未定义(0),并且"if"子句将永远不会执行.

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

这篇关于Azure移动服务,自定义API,更新输出并随后选择返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆