小编Mai*_*uiz的帖子

使用mongoose验证整数值

我正在使用mongoose,我需要定义如下的模式:

const QuestionSchema = new Schema({
    text: {
        type: String,
        required: true
    },
    number: {
        type: Number,
        required: true,
        unique: true
    }
});
Run Code Online (Sandbox Code Playgroud)

我需要验证该number字段是一个整数值.我见过几个插件,例如:mongoose-integer:

const validateInteger = require('mongoose-integer');
const QuestionSchema = new Schema({
    text: {
        type: String,
        required: true
    },
    number: {
        type: Number,
        required: true,
        unique: true,
        integer: true
    }
});
QuestionSchema.plugin(validateInteger);
Run Code Online (Sandbox Code Playgroud)

但我不想使用任何第三方库.还有其他方法吗?提前致谢.

mongoose mongodb node.js

15
推荐指数
1
解决办法
1万
查看次数

在现代接力中将变量传递给片段容器

我正在使用Relay Modern(compat).我有一个片段,其中包含一个具有一个参数的字段,但我找不到从父组件传递变量值的方法:

// MyFragmentComponent.jsx

class MyFragmentComponent extends Component {...}

const fragments = {
  employee: graphql`
    fragment MyFragmentComponent_employee on Employee {
      hoursWorked(includeOvertime: $includeOvertime)
      dob
      fullName
      id
    }
  `,
}

export default Relay.createFragmentContainer(MyFragmentComponent, fragments)
Run Code Online (Sandbox Code Playgroud)

它最终会说$includeOvertime没有定义.渲染此组件的上下文如下所示:

// MyRootComponent.jsx

class MyRootComponent extends Component {
  render() {
    const { employee } = this.props
    const includeOvertime = //... value is available here

    return (
      <div>
        <MyFragmentComponent employee={employee} />
      </div>
    )
  }
}

const query = graphql`
  query MyRootComponentQuery($employeeId: String!) {
    employee(id: $employeeId) …
Run Code Online (Sandbox Code Playgroud)

relayjs relaymodern

9
推荐指数
1
解决办法
4161
查看次数

在 express 中间件中记录请求和响应

我正在尝试在Express应用程序中实现记录器。我需要它能够记录请求和为每个请求发回的响应(状态代码和正文)。我开始编写一个看起来像这样的中间件:

function (req, res, next) {
    ...
    res.on('finish', function () {
       Logger.debug('For request', req);
       Logger.debug('Response sent');
    });
    ...
}
Run Code Online (Sandbox Code Playgroud)

我需要访问传递给res用于发送响应的对象方法的数据。例如,如果在一个控制器中我有:

res.json({ foo: 'bar' })
Run Code Online (Sandbox Code Playgroud)

我需要一种方法来获取该{ foo: 'bar' }对象,可能是这样的:

function (req, res, next) {
    ...
    res.on('finish', function () {
       Logger.debug('For request', req);
       var data = res.data; // or res.body, or whatever
       Logger.debug('Response: ' + res.statusCode, data);
    });
    ...
}
Run Code Online (Sandbox Code Playgroud)

res我可以使用Express对象中的任何属性或方法吗?或者,是否有更好的策略来记录请求及其响应?

javascript logging node.js express

7
推荐指数
2
解决办法
9188
查看次数

对多个字段使用 @include 指令

我有一个中继容器,可以使用 有条件地获取某些字段@include(if: $variable),但我必须为每个单独的字段编写指令:

const relayOptions = {
  initialVariables: {
    expandDetails: false,
  },
  fragments: {
    company: Relay.QL`
      fragment on Company {
        id
        name
        employees @include(if: $expandDetails) {
          fullName
        }
        admins @include(if: $expandDetails) {
          fullName
        }
        departments @include(if: $expandDetails) {
          name
        }
      }
    `,
  },
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以让我只编写一次指令,例如这样的东西(而且我知道这段代码不起作用):

const relayOptions = {
  initialVariables: {
    expandDetails: false,
  },
  fragments: {
    company: Relay.QL`
      fragment on Company {
        id
        name
        @include(if: $expandDetails) {
          employees {
            fullName
          }
          admins {
            fullName
          }
          departments {
            name …
Run Code Online (Sandbox Code Playgroud)

graphql relayjs

0
推荐指数
1
解决办法
1634
查看次数