所以我有nodejs http handler接受JSON input并将其解析为recursive class。然后handler遍历类的内容,将数据填充到适当的列,表单和子表单中。
如果此works fine没有现有记录。
util.get('/', function (req, res) {
var requestBody = '';
var msg;
var curform;
req.on('data', function (data) {
requestBody += data;
});
req.on('end', function () {
var ld = new Loading(null, JSON.parse(requestBody));
Login(ld, curform).then(() => {
res.simpleJSON(200, ld.log.toJSON)
}).catch(() => {
res.simpleJSON(200, ld.log.toJSON)
})
});
function Login(ld, curform) {
return new Promise((resolve, reject) => {
console.log("Connecting...");
priority.login(config).then(() => {
console.log("Opening form %s.", ld.name);
priority.formStart(ld.name, null, null, 1).then(curform => {
curform.clearRows().then(() => {
iter(ld.rows, iterRow, curform).then(curform => {
console.log("Close form %s.", curform.name);
curform.endCurrentForm().then(() => {
resolve()
}).catch(err => { ld.log.setError(null, null, err); reject(err) })
}).catch(err => { ld.log.setError(null, null, err); reject(err) })
}).catch(err => { ld.log.setError(null, null, err); reject(err) })
}).catch(err => { ld.log.setError(null, null, err); reject(err) })
}).catch(err => { ld.log.setError(null, null, err); reject(err) })
})
}
var iterRow = function (m, curform) {
return new Promise((resolve, reject) => {
iter(m.columns, iterField, curform).then(curform => {
console.log("Saving form %s.", curform.name);
curform.saveRow(0).then(() => {
m.sucsess();
iter(m.subLoadings, iterForm, curform).then(curform => {
if (m.$index < m.parent.rows.length-1) {
console.log("New row in form %s.", curform.name);
curform.newRow().then(() => {
resolve(curform)
}).catch(err => reject(err))
} else {
resolve(curform)
}
}).catch(err => reject(err))
}).catch(err => {
m.fail(err);
resolve(curform);
})
}).catch(err => reject(err))
})
}
var iterForm = function (m, curform) {
return new Promise((resolve, reject) => {
console.log("Opening sub form %s.", m.name);
curform.startSubForm(m.name, null, null).then(curform => {
curform = curform;
console.log("New row in form %s.", curform.name);
curform.newRow().then(() => {
iter(m.rows, iterRow, curform).then(curform => {
console.log("Closing sub form %s.", curform.name);
curform.endCurrentForm().then(() => {
resolve(curform);
}).catch(err => reject(err))
}).catch(err => reject(err))
}).catch(err => reject(err))
}).catch(err => reject(err))
})
};
var iterField = function (m, curform) {
return new Promise((resolve, reject) => {
console.log("Set field %s = %s.", m.name, m.value);
curform.fieldUpdate(m.name, m.value).then(() => {
resolve(curform);
}).catch(err => {
if (err.message == "Record already exists in form.") {
SelectRow(m, curform).then(() => {
resolve();
}).catch(err => reject(err))
} else {
reject(err)
}
})
})
}
/* Iterate function */
function iter(ar, fn, result) {
return new Promise((resolve, reject) => {
try {
var cntr = 0;
function next() {
if (cntr < ar.length) {
ar[cntr].$index = cntr;
fn(ar[cntr], result).then(curform => {
cntr++;
next();
}).catch(err => reject(err));
} else {resolve(result)};
};
next();
} catch (err) { reject(err)}
})
}
});
但是,如果记录已存在于表单中,fieldUpdate当然会抛出"Record already exists in form."
错误。问题是如何最好地处理这个问题。
在父表单中,我只需使用ClearRows,这样fieldUpdate将打开记录(如果存在)。但这似乎不适用于子表格。
同样,我可以使用setSearchFilter和setActiveRow(如果newRow的长度为零,则仅调用getRows)但同样,setSearchFilter似乎只在父表单中支持。
鉴于这些限制,我还考虑对此错误使用未经过滤的getRows,以识别现有记录,并setActiveRow这样:
function SelectRow(m, curform) {
return new Promise((resolve, reject) => {
curform.undo().then(() => {
curform.getRows(1).then(rows => {
var cntr = 1;
while (rows[curform.name][cntr] !== undefined) {
var f = 1;
for (var i in parent.rows[m.$index].columns) {
if (rows[cntr][parent.rows[m.$index].columns[i].name] !== parent.rows[m.$index].columns[i].value) {
f = 0;
}
}
if (f == 1) {
curform.setActiveRow(cntr).then(() => {
resolve();
})
} else { cntr++; }
}
resolve();
}).catch(err => reject(err))
}).catch(err => reject(err))
})
}
但是调用undo方法getRows函数后却没有返回任何结果。
有什么建议吗?