使用带Q承诺nodejs的数组字段迭代JSON数组
我对Q的承诺是新的。 我有一个像这样的input结构的JSON:
[{"code": x, "status": y, "array": [ { "value1": k }, { "value2": z } },{...} ...]
现在我必须用ObjectId引用将它保存在MongoDB的两个不同的集合中; 所以我想要这样的东西:
C1: {"_id": 1,"code": x, "status": y, "array": [2,3]} C2: {"_id": 2,"value1": k},{"_id":3, "value2": z}
我尝试迭代两个数组映射,如下所示:
Q.all(codes.map(function(currCode) { return this.saveNewCode(currCode) .then(function(code) { return Q.all(currCode.array.map(function(currElem) { return this.saveNewElem(currElem) .then(function(elem) { return this.addElemToCode(elem.id,code.id) }) })) }) }))
在这种方式返回是一个地图[[]],我想返回像最初的JSON响应。