是什么导致我的测试返回false?

时间:2012-11-12 04:52:36

标签: javascript alfresco alfresco-share

我在Alfresco的回购方设置了一个webscript。 webscript使用POST方法,接受并返回json,并且仅用于设置一些属性以支持客户端ajax应用程序。 脚本的控制器代码如下:

/**
 * Document Bundle Management Property setter method
 *
 * @method POST
 */

function main() {
        var nodeRef=args.doc,
            salesOrderNumber=json.get("salesOrderNumber"),
            stockCode=json.get("stockCode"),
            VIN=json.get("VIN"),
            customerCode=json.get("customerCode"),
            resultObj={result:"failure",problems:[],debug:[]};
        resultObj.debug.push("Webscript begins. Variables:");
        resultObj.debug.push("\tsalesOrderNumber: " + salesOrderNumber);
        resultObj.debug.push("\tstockCode: " + stockCode);
        resultObj.debug.push("\tVIN: " + VIN);
        resultObj.debug.push("\tcustomerCode:" + customerCode);
        if (nodeRef) {
                resultObj.debug.push("Received nodeRef variable. Checking validity");
                doc=utils.getNodeFromString(nodeRef);
                if (doc && doc.isDocument) {
                        resultObj.result="success";
                        resultObj.debug.push("nodeRef valid, checking document type");
                        if (doc.isSubType("my:documentBundle")) {
                                resultObj.debug.push("Document is already of type my:documentBundle. Not specializing");
                        } else {
                                resultObj.debug.push("Document is not of type my:documentBundle. Specializing");
                                doc.specializeType("my:documentBundle");
                        }
                        if (salesOrderNumber && salesOrderNumber.length && salesOrderNumber.length>0) {
                                doc.properties["salesOrderNumber"]=salesOrderNumber;
                                resultObj.debug.push("Set document salesOrderNumber to " + salesOrderNumber);
                        } else {
                                resultObj.debug.push("salesOrderNumber was not received, was blank or of zero length. ignoring");
                        }
                        if (stockCode && stockCode.length && stockCode.length>0) {
                                doc.properties["stockCode"]=stockCode;
                                resultObj.debug.push("Set document stockCode to " + stockCode);
                        } else {
                                resultObj.debug.push("stockCode was not received, was blank or of zero length. ignoring");
                        }
                        if (VIN && VIN.length && VIN.length>0) {
                                doc.properties["VIN"]=VIN;
                                resultObj.debug.push("Set document VIN to " + VIN);
                        } else {
                                resultObj.debug.push("VIN was not received, was blank or of zero length. ignoring");
                        }
                        if (customerCode && customerCode.length && customerCode.length>0) {
                                doc.properties["customerCode"]=customerCode;
                                resultObj.debug.push("Set document customerCode to " + customerCode);
                        } else {
                                resultObj.debug.push("customerCode was not received, was blank or of zero length. ignoring");
                        }
                        doc.save();
                        resultObj.debug.push("document saved");
                } else {
                        resultObj.problems.push("Invalid nodeRef supplied!");
                }
        } else {
                resultObj.problems.push("No nodeRef supplied!");
        }
        model.obj=resultObj;
}

main();

我的问题发生在4行中,如:if (salesOrderNumber && salesOrderNumber.length && salesOrderNumber.length>0) {,即使我确定变量包含的字符串长度不是零,它总是会评估为false。如果我查看返回的debug属性,我会看到以下内容:

0: "Webscript begins. Variables:"
1: "    salesOrderNumber: test"
2: "    stockCode: test"
3: "    VIN: test"
4: "    customerCode:test"
5: "Received nodeRef variable. Checking validity"
6: "nodeRef valid, checking document type"
7: "Document is already of type my:documentBundle. Not specializing"
8: "salesOrderNumber was not received, was blank or of zero length. ignoring"
9: "stockCode was not received, was blank or of zero length. ignoring"
10: "VIN was not received, was blank or of zero length. ignoring"
11: "customerCode was not received, was blank or of zero length. ignoring"  
12: "document saved"

所以,我可以看到所有四个变量的值都是“test”。如果在firebug或js控制台中我输入

a="test";
if (a && a.length && a.length>0) then console.log("true") else console.log("false")

我得到了正确的输出,正如预期的那样。 我没有看到可能会影响到这一点 - 我几乎每天都使用这种令人难以置信的基本条件来测试字符串,但是这次我显然错过了一些东西。帮助

1 个答案:

答案 0 :(得分:1)

编辑:根据您的评论,我有一个新的理论。由于这些似乎是某种对象而不是原始字符串,length方法正在造成麻烦:

此简单测试的行为与您的问题完全相同:

var x = {
    toString: function() { return 'test'; },
    length: function() { return 'something'; }
};

console.log('x is: ' + x);
if (x && x.length && x.length > 0)
    console.log('passed');
else
    console.log('failed');

当您将其连接到字符串时报告为“test”,但它未通过测试,因为length > 0为false 因为length是函数。我建议将其转换为字符串。


另外,我要指出你的一些测试是不必要的。当且仅当salesOrderNumber.length > 0为假时,salesOrderNumber.length才会为假。同样地,当且仅当salesOrderNumber.length为空字符串(对于字符串)时,salesOrderNumber才会是假的,因此您可以简单地:

salesOrderNumber = String(salesOrderNumber);
if (salesOrderNumber)
    console.log('ok');
else
    console.log('oops');