编写查询以从多个值添加键

时间:2014-09-16 14:53:11

标签: node.js coffeescript redis

我正在解析一个DBF文件,就像

"{\"srCode\":\"EUCRDCTN\",\"accountCode\":\"\",\"priceList\":\"EUCN\",\"discount\":null,\"termDays\":30}"
"{\"srCode\":\"\",\"accountCode\":\"MEN006\",\"priceList\":\"EUSD\",\"discount\":10,\"termDays\":null}"

在某些地方我在srCode中有一个值,而在其他地方我有它在accountCode

将此插入到一个redis集中的正确方法是什么,因此,我得到类似的内容:

我在redis-cli上做了这个。

☺  redis-cli                                                                                                                                                               2.1.0""
redis 127.0.0.1:6379> HMSET test "MEN006" "{\"srCode\":\"\",\"accountCode\":\"MEN006\",\"priceList\":\"EUSD\",\"discount\":10,\"termDays\":null}"
OK
redis 127.0.0.1:6379> HMSET test "GBCRDCTN" "{\"srCode\":\"EUCRDCTN\",\"accountCode\":\"\",\"priceList\":\"EUCN\",\"discount\":null,\"termDays\":30}"
OK

redis 127.0.0.1:6379> hgetall "test"
1) "GBCRDCTN"
2) "{\"srCode\":\"EUCRDCTN\",\"accountCode\":\"\",\"priceList\":\"EUCN\",\"discount\":null,\"termDays\":30}"
3) "MEN006"
4) "{\"srCode\":\"\",\"accountCode\":\"MEN006\",\"priceList\":\"EUSD\",\"discount\":10,\"termDays\":null}"
redis 127.0.0.1:6379>

这有效,但在我的代码中,这是coffeescript,我有

exports.router = () ->
  express.Router()
    .post "/buyers", buyers
    .use (req,res) -> res.status(404).send("Invalid API Call")

buyers = (req, res, next) ->
  console.log req.body
  buyersMap = _(req.body).map( (r) -> [r.accountCode, JSON.stringify(r)] ).zipObject().value()
  srCodeMap = _(req.body).map( (r) -> [r.srCode, JSON.stringify(r)] ).zipObject().value()
  hmsetPr "buyers", buyersMap
  hmsetPr "buyers", srCodeMap
    .then (result) -> res.status(200).send("Ok")
    .catch next

所以,如果我搜索

redis 127.0.0.1:6379> hget "buyers" "EUCRDSTD"
(nil)

但如果我将代码更改为hmsetPr "terms", srCodeMap,我会得到一个结果

redis 127.0.0.1:6379> hget "terms" "EUCRDSTD"
"{\"srCode\":\"EUCRDSTD\",\"accountCode\":\"\",\"priceList\":\"EUSD\",\"discount\":null,\"termDays\":30}"

我错过了什么?

任何建议非常感谢

1 个答案:

答案 0 :(得分:0)

好吧,我的代码应该是:

buyers = (req, res, next) ->
  buyersMap = _(req.body).map((r) ->
    if r?.srCode then [r.srCode, JSON.stringify(r)]
    [r.accountCode, JSON.stringify(r)]
  ).zipObject().value()
  hmsetPr("buyers", buyersMap).then((result) ->
    res.status(200).send "Ok"
  )["catch"] next