node.js - 编写查询以从多个值添加键

标签 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}"

我错过了什么?

非常感谢任何建议

最佳答案

好的,我的代码应该是:

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

关于node.js - 编写查询以从多个值添加键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25872024/

相关文章:

node.js - 安装 npm 时出现警告

javascript - Promise Chain Breaks w/.all()

node.js - 为什么 npm start 抛出 events.js :187 throw er;//Unhandled 'error' event in my react project?

javascript - 嵌套匿名函数中的 CoffeeScript 类属性

coffeescript - 即使存在 coffeescript 编译错误,我如何保持 grunt 服务器运行?

c# - 微服务架构 CQRS

node.js - NodeJs - Bluebird promise.resolve(value) 未定义

javascript - 无法在 Coffeescript 上的 Rickshaw Graph 上显示月份名称

kubernetes - 将 redis-sentinel 连接到 Kubernetes 上的 redis-master

java - Jedis:Bean 属性 'maxActive' 不可写或具有无效的 setter 方法