MassiveJS奇怪的投射错误

时间:2016-07-10 14:51:53

标签: javascript massivejs

我是Massive的新手,但我真的很喜欢它。在使用express.Router()并进行非常简单的数据库调用时:

router.get('/:id', function(req, res, next) {  
  db.accounts.find(req.params.id, function(err, results) {...});

我收到了一个错误:

  

错误:参数0(条件)应该是Object类型,但它是类型   值为2的字符串。       在Args(C:\ Users \ JMichelson \ WebstormProjects \ Proximityv6 \ node_modules \ args-js \ Args.js:399:10)       at Object.exports.forArgs(C:\ Users \ JMichelson \ WebstormProjects \ Proximityv6 \ node_modules \ massive \ lib \ arg_types.js:7   7:10)......

通过简单的演员解决了这个问题:

router.get('/:id', function(req, res, next) {  
  db.accounts.find(Number(req.params.id), function(err, results) {...});

但我发现这个转换要求很奇怪,因为JavaScript应该根据需要自动转换。

我这样做是否正确?

1 个答案:

答案 0 :(得分:1)

I believe that find wants a number and the params come in as a string so the args parser gets confused. You can use int.parse here or do what you're doing. Massive is a bit opinionated in this regard: you can use string keys if you want but you'll have to be specific with `{id:"my string"}.

相关问题