Domanda

I have a webscript set up on the repo-side in Alfresco. The webscript uses the POST method, accepts and returns json and is simply designed to set some properties to support a client-side ajax app. The controller code for the script is as follows:

/**
 * 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();

My problem occurs in the 4 lines like: if (salesOrderNumber && salesOrderNumber.length && salesOrderNumber.length>0) {, which always seem to evaluate false even when I am sure the variable contains a string that is not zero length. If I look at the debug property returned I see the following:

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"

So, I can see that all four variables have the value "test". If in firebug or the js console I enter

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

I get true output, as expected. I don't see what could be affecting this - I use this incredibly basic condition almost daily to test strings before I use them but I am obviously missing something this time. help?

È stato utile?

Soluzione

Edit: Based on your comment below, I have a new theory. Since these appear to be some sort of objects and not primitive strings, the length method is what's causing the trouble:

This simple test behaves exactly the same way as your problem:

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

It reports to be "test" when you concatenate it to a string, but it fails the test because length > 0 is false because length is a function. I would recommend converting it to a string.


Also, I would point out that some of your tests are unnecessary. salesOrderNumber.length > 0 will be false if and only if salesOrderNumber.length is falsey. Likewise, salesOrderNumber.length will only be falsey if and only if salesOrderNumber is the empty string (for a string), so you can simply:

salesOrderNumber = String(salesOrderNumber);
if (salesOrderNumber)
    console.log('ok');
else
    console.log('oops');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top