gpt4 book ai didi

node.js - 当没有找到记录时,Sequelize findone 查询返回 NULL,我可以将此 NULL 更改为我所有 findOne 查询的空对象吗?

转载 作者:行者123 更新时间:2023-12-03 22:35:52 25 4
gpt4 key购买 nike

我可以在没有数据匹配的情况下将 findOne 查询结果从 NULL 更改为空对象吗?如果是,如何?
先感谢您

最佳答案

我认为此选项的 Sequelize 中没有插入配置。

您可以为模型访问层创建一个基本的 DAL 类。

export class BaseDal {
constructor(model) {
this.model = model;
}

async function findOne(filter, options = {}) {
let res = await this.model.findOne(filter, option);
return !res ? {} : res;
}
}

然后与您的任何模型相关的每个 dal 都将从此类继承:

import {BaseDal} from './base-dal.js'

class SomeModelDal extends BaseDal {
constructor(model) {
super(model);
}

function findEntityById(id) {
let filter = {id: id};
return this.findOne(filter);
}
}

关于node.js - 当没有找到记录时,Sequelize findone 查询返回 NULL,我可以将此 NULL 更改为我所有 findOne 查询的空对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com