带有环回的并发ID生成

时间:2018-10-17 16:42:47

标签: node.js loopbackjs strongloop

我尝试为某些模型生成唯一标识符,但是如果我从客户端向API发送两个或多个CREATE请求,则最终会获得相同的ID

`

Model.observe('before save', function(
  ctx, next) {
  Model.find({
        where: {
          parentId: parentId
        }
      }, limit: 1, order:
      'numberIndex DESC'
    },
    function(err, res) {
      let idx = res[0].numberIndex + 1;
      ctx.instance['identifier'] = 'SomePrefix' + idx;
      ctx.instance['numberIndex'] =idx;
      next()
    })
})
`

0 个答案:

没有答案
相关问题