我有一段代码:
async _addStructure() {
const {pmo} = this.state;
let struct = null;
try {
struct = await structureService.getWhere({
name: pmo
})
} catch (e) {
return null
}
if (struct.length) {
return struct[0]
}
try {
const structure = await structureService.create(new Structure(pmo));
toastr.success('Succès', 'succès');
return structure
} catch (e) {
return null
}
}得到一个结构,如果为null,则创建一个新结构。
ReactJS中是否有任何可以处理它的现有方法。在NodeJS中,我们似乎可以:
return Structure.findOrCreate({where: {name: pmo}}, {
"name": pmo,
}) 要干净得多。
我在网上找不到任何东西,这可能意味着这是不可能的,只是在寻找一个确认!
发布于 2020-06-05 13:21:38
您希望“获得一个结构,如果为null,则创建一个新的结构。”
看起来像lodash.set,它与lodash.get很相配
// API
_.set(object, path, value)
const object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5在对象路径上设置值。如果路径的一部分不存在,则创建.
。
https://stackoverflow.com/questions/62216226
复制相似问题