无法使用worklight从SQL Adapter获取PRIMARY_KEY违规响应?

时间:2014-06-16 09:26:23

标签: ibm-mobilefirst worklight-adapters

我有MySql表,其中包含userID,Mobile,Password,Re-Password列,其中Mobile Column是我的PRIMARY_KEY,我正在从我的SQL Adapter中插入数据。

SQLAdapter-impl.js

function registration(mob_no,user_name,pass,re_pass){
    return WL.Server.invokeSQLStoredProcedure({
        procedure : "registration",
        parameters : [mob_no,user_name,pass,re_pass]

    });
}

SQLAdapter.xml

<procedure name="registration" />

Registration.js

function registration(mob_no,user_name,pass,re_pass){
    var invocationData = {
            adapter : "SQLAdapter",
            procedure: "registration",
            parameters: [mob_no,user_name,pass,re_pass]
    };
    WL.Client.invokeProcedure(invocationData, {
        onSuccess: getSecretDataOKReg, 
        onFailure: getSecretDataFAILReg
    });
}

function getSecretDataOKReg(response)
{   
    var bool=response.invocationResult.isSuccessful;
    console.log("Registration Operation Successfull :" +bool);
    if(bool)
    {
        saveRegistrationDetails();
        $("#pagePort").load(path + "pages/Login.html", function(){
            $.getScript(path + "js/Login.js", function() {
                if (currentPage.init) {
                    currentPage.init();
                }
            });
        });
    }
}

function getSecretDataFAILReg(response)
{
    alert("Unsuccess "+response);
}

我的问题是当我插入新数据时,一切顺利,我得到适当的回应,一切都是&#34; Happie Ending Story&#34;但是当我试图插入相同的手机号码以及不同的permo-combo数据时,我得到错误

Procedure invocation error. Runtime: Failed to retrieve data with procedure : registration worklight.js:4673
WL.Logger.__log worklight.js:4673
PUBLIC_API.(anonymous function) worklight.js:4858
onInvokeProcedureSuccess worklight.js:7355
window.WLJSX.Ajax.WLRequest.WLJSX.Class.create.onSuccess worklight.js:3238
window.WLJSX.Ajax.WLRequest.WLJSX.Class.create.onWlSuccess worklight.js:3210
(anonymous function) worklight.js:947
window.WLJSX.Ajax.Request.WLJSX.Class.create.respondToReadyState worklight.js:1156
window.WLJSX.Ajax.Request.WLJSX.Class.create.onStateChange worklight.js:1094
(anonymous function)

据我所知,这可能已经到了,因为我试图在MYSql中插入相同的主键数据,但我怎么能肯定地处理它并向用户显示警告或说明你已经注册?我的js文件中是否有任何问题来识别错误响应中无法看到的SQL错误代码。

1 个答案:

答案 0 :(得分:0)

此错误与Worklight在这里没有任何问题我认为你应该使用从MySQL返回值的Callback。

function registration(mob_no,user_name,pass,re_pass,returnValue){
    var invocationData = {
            adapter : "SQLAdapter",
            procedure: "registration",
            parameters: [mob_no,user_name,pass,re_pass,returnValue]
    };
    WL.Client.invokeProcedure(invocationData, {
        onSuccess: getSecretDataOKReg, 
        onFailure: getSecretDataFAILReg
    });
}

function getSecretDataOKReg(response)
{   
    var bool=response.invocationResult.isSuccessful;
    console.log("Registration Operation Successfull :" +bool);
    if(bool)
    {
        saveRegistrationDetails();
        $("#pagePort").load(path + "pages/Login.html", function(){
            $.getScript(path + "js/Login.js", function() {
                if (currentPage.init) {
                    currentPage.init();
                }
            });
        });
    }
}

function getSecretDataFAILReg(response)
{   
    console.log("Check this here "+ response.invocationResult.resultSet[0].returnValue);
    alert("Unsuccess "+JSON.stringify(response));
}

并在您的存储过程中使用returnValue作为OUT参数并使用if和else条件。

请找到相同的SQL存储过程:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `loginAuthentication`(IN mob_no VARCHAR(45),IN user_name VARCHAR(45),pass VARCHAR(45),re_pass VARCHAR(45),OUT returnvalue INT)

BEGIN
       DECLARE no_of_records INT;

       SELECT COUNT(*) INTO no_of_records
       FROM tablename.registration 
       WHERE tablename.registration.mob_no=mob_no ;

       IF no_of_records = 1 THEN 
       SET returnvalue = 0; //Already exists one record with mob_no
       ELSE

       // Your INSERT QUERY

       SET returnvalue = 1;
       END IF;
END $$
DELIMITER ;