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