0

我在 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。如果我查看返回的调试属性,我会看到以下内容:

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”。如果在萤火虫或 js 控制台我输入

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

正如预期的那样,我得到了真正的输出。我看不出是什么影响了这一点——我几乎每天都使用这个令人难以置信的基本条件来测试字符串,然后再使用它们,但这次我显然遗漏了一些东西。帮助?

4

1 回答 1

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

当您将它连接到一个字符串时,它报告为“测试”,但它没有通过测试,因为它length > 0是错误的,因为长度是一个函数。我建议将其转换为字符串。


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

salesOrderNumber = String(salesOrderNumber);
if (salesOrderNumber)
    console.log('ok');
else
    console.log('oops');
于 2012-11-12T05:19:15.980 回答