node.js - 函数参数 - id 与对象

标签 node.js typescript function parameter-passing typescript-typings

我有一些服务可以通过 id 返回用户的数据。 用户已存储在请求( session )中 - 无需每次都获取它。

问题是参数的最佳实践是什么。用户的 ID (SOLID) 或用户对象(更多验证/输入)。

示例:

//...

// this
public async getByUser(user: User) {
  return await Transaction.findAll({
    where: { userId: user.id }
  });
}

// or this
public async getByUser(userId: number) {
  return await Transaction.findAll({
    where: { userId }
  });
}

最佳答案

要么。真的。 只要保持命名正确即可。

函数名称越具有声明性,困惑就越少

//...

// this
public async getByUser(user: User) {
  return await Transaction.findAll({
    where: { userId: user.id }
  });
}

// or this
public async getByUserId(userId: number) {
  return await Transaction.findAll({
    where: { userId }
  });
}

关于node.js - 函数参数 - id 与对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810121/

相关文章:

javascript - 在 json 字符串中添加父级别

javascript - @testing-library/react (rtl) 'waitFor' 只有在没有 await 关键字的情况下才会成功

javascript - 如何在 ionic 应用程序中设置 setCancelable(false)?

php - 如何连接两个数组键?

Node.JS - 如何从任何路径运行 Node 命令?

javascript - Jade /Expressjs : Pass objects from server to client

node.js - 导出类与函数

angular - 从另一个组件打开/关闭 sidenav

matlab - 如何编写没有循环的程序

c - 函数调用的最小深度