I am parsing a DBF file, which is like
"{\"srCode\":\"EUCRDCTN\",\"accountCode\":\"\",\"priceList\":\"EUCN\",\"discount\":null,\"termDays\":30}"
"{\"srCode\":\"\",\"accountCode\":\"MEN006\",\"priceList\":\"EUSD\",\"discount\":10,\"termDays\":null}"
in some places i have a value in srCode and in other i have it in accountCode
what is the correct way to insert this into one redis set, so that, i get something like:
i did this on the 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>
this works, but in my code, which is coffeescript, i have
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
so if i search for
redis 127.0.0.1:6379> hget "buyers" "EUCRDSTD"
(nil)
but if i change the code to hmsetPr "terms", srCodeMap i get a result
redis 127.0.0.1:6379> hget "terms" "EUCRDSTD"
"{\"srCode\":\"EUCRDSTD\",\"accountCode\":\"\",\"priceList\":\"EUSD\",\"discount\":null,\"termDays\":30}"
what am i missing?
any advise much appreciated
ok, my code should be:
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