我有pouchdb / couchbase数据与设备已分配给他们的设备。 带有_id的设备和设备doc中有一个checkedOutBy,其中user._id为值。在employee对象中有user.name。当我获得设备对象时,如何获取user.name并显示设备。
我已经搜索并阅读了使用发射的地图/缩小,并且没有掌握这个想法。我从我学到的东西写的代码是: 顺便说一句,我也在使用Angularjs。
field = "eq::"
this.getAllEquip = function(field){
function map(doc) {
if (doc.checkedOutBy !== undefined) {
emit(doc.checkedOutBy, {empName : doc.name});
}
}
var result = database.query(map, {include_docs: true,
attachments: true,
startkey: field,
endkey: field + '\uffff'})
.catch(function (err) {
//error stuff here
});
return result
};
我不知道两个文档会在哪里聚在一起。我错过了什么?我的结果是空的。
设备json看起来像:
{checkedOutBy:" us :: 10015&#34 ;, description:" 3P Microsoft Surface w / stylus&电源线",equipId:" SUR1501",purchaseDate:"",rCost:1000,id:" eq :: 10001"}
Emlpoyee json:{"姓名":"乔""两性":"男性""姓":& #34;吹塑""状态":"有源""标题":"办公""类型&# 34;:" USERINFO"" _id":"我们:: 10015"" _rev":" 2-95e9f34784094104ad24bbf2894ae786&# 34;}
感谢您的帮助。
答案 0 :(得分:1)
如果我理解正确的问题,这样的事情应该有用:
//Sample Array of Objects with Equipment
var arr1=[{checkedout:"abc1",desc:"item1",id:1},
{checkedout:"abc2",desc:"item2",id:2},
{checkedout:"abc3",desc:"item3",id:3},
{checkedout:"abc1",desc:"item1",id:4},
{checkedout:"abc4",desc:"item3",id:5},
{checkedout:"abc6",desc:"item3",id:6}];
//Sample array of objects with Employee - the "id" in arr2 matches with "checkout" in arr1
var arr2=[{name:"john",id:"abc1"},
{name:"jack",id:"abc2"},
{name:"alice",id:"abc3"},
{name:"james",id:"abc4"}];
var result = []; //final result array
//loop through equipment array arr1
arr1.forEach(function(obj) {
var tempObj = obj;
var checkedout_id=obj.checkedout;
//do array.find which will return the first element in the array which satisfies the given function. This is absed on the assumption that that the id is unique for employee and there wont bwe multiple employees with same id (which is the "checkedout" field in equipment. If the employee is not found, it will return undefined.
var foundname = arr2.find(function(obj) {
if (obj.id == checkedout_id)
return obj.name
})
//Create the object to be inserted into the final array by adding a new key called "name", based on the result of above find function
if (foundname != undefined) {
tempObj.name=foundname.name
}
else {
tempObj.name = "Not found";
}
result.push(tempObj);
})
答案 1 :(得分:0)
这是我的Pouchdb解决方案,感谢Vijay带领我找到这个解决方案。 首先,我得到了所有的设备。然后我使用Vijay的想法循环遍历数组并将名称添加到对象并构建新数组。我发现有必要进入.doc。对象的一部分,如obj.doc.checkedOutBy和tempObj.doc.name,以完成工作。
$pouchDB.getAllDocs('eq::').then(function(udata){
var result = [];
//loop through equipment array
udata.rows.forEach(function(obj) {
var tempObj = obj;
var checkedout_id=obj.doc.checkedOutBy;
if (checkedout_id != undefined) {
$pouchDB.get(checkedout_id).then(function(emp){
return emp.firstname + " " + emp.lastname
}).then(function(name){
tempObj.doc.name = name;
});
}
result.push(tempObj);
})
在我的服务中我有:
this.get = function(documentId) {
return database.get(documentId);
};
和
this.getAllDocs = function(field){
return database.allDocs({
include_docs: true,
attachments: true,
startkey: field,
endkey: field + '\uffff'});
};