这段代码解析三元组(不那么重要)并且应该一次返回一个停车位。问题是调用observer.next()会打破循环,因为它只运行一次。 console.log也没有被调用。我可能遗漏了一些东西但是有可能是observer.next()打破了循环吗?是否有错误或是一个功能还是我错了?
return Observable.create(observer => {
this.fetch.get(datasetUrl).then(response => {
// Get all subjects that are parkings
const parkingTriples = [],
parkings = [],
totalspacesParking = [],
labels = [];
for (let index = 0; index < response.triples.length; index++) {
if (response.triples[index].object === 'http://vocab.datex.org/terms#UrbanParkingSite') {
parkingTriples.push(response.triples[index]);
}
if (response.triples[index].predicate === 'http://vocab.datex.org/terms#parkingNumberOfSpaces') {
totalspacesParking.push(response.triples[index]);
}
if (response.triples[index].predicate === 'http://www.w3.org/2000/01/rdf-schema#label') {
labels.push(response.triples[index]);
}
}
const _parkings = [];
for (let index = 0; index < parkingTriples.length; index++) {
const totalspacesresult = find(totalspacesParking, (o) => {
return o.subject === parkingTriples[index].subject
});
const totalspaces = parseInt(n3.Util.getLiteralValue(totalspacesresult.object), 10);
const labelresult = find(labels, (o) => {
return o.subject === parkingTriples[index].subject
});
const rdfslabel = n3.Util.getLiteralValue(labelresult.object);
const id = rdfslabel.replace(' ', '-').toLowerCase();
observer.next(new Parking(rdfslabel, parkingTriples[index].subject, id, totalspaces, datasetUrl));
console.log(observer);
}
})
})
答案 0 :(得分:1)
通常,Subject
不会破坏您的代码。鉴于以下内容:
const observer = new Rx.Subject();
const parkingTriples = new Array(10);
observer.subscribe(val => console.log('value emitted: ' + val));
for (let index = 0; index < parkingTriples.length; index++) {
observer.next(index);
console.log('I still get hit through');
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.2/Rx.js"></script>
&#13;
每次都会发出I still get hit through
。最有可能的是observer
未在您的代码中定义,因此调用observer.next()
中断并且永远不会看到以下console.log
。
答案 1 :(得分:1)
鉴于您更新的代码我建议调试它。很可能这个bug不在Rx位中。我重构了你的代码,以便在Rx逻辑和响应解析之间进行分割:
function getParkings(datasetUrl){
return Rx.Observable.defer(() => this.fetch.get(datasetUrl))
.mergeMap(response => parseParkingResponse(response));
}
function parseParkingResponse(response) {
const parkingTriples = [],
parkings = [],
totalspacesParking = [],
labels = [];
for (let index = 0; index < response.triples.length; index++) {
if (response.triples[index].object === 'http://vocab.datex.org/terms#UrbanParkingSite') {
parkingTriples.push(response.triples[index]);
}
if (response.triples[index].predicate === 'http://vocab.datex.org/terms#parkingNumberOfSpaces') {
totalspacesParking.push(response.triples[index]);
}
if (response.triples[index].predicate === 'http://www.w3.org/2000/01/rdf-schema#label') {
labels.push(response.triples[index]);
}
}
const _parkings = [];
for (let index = 0; index < parkingTriples.length; index++) {
const totalspacesresult = find(totalspacesParking, (o) => {
return o.subject === parkingTriples[index].subject
});
const totalspaces = parseInt(n3.Util.getLiteralValue(totalspacesresult.object), 10);
const labelresult = find(labels, (o) => {
return o.subject === parkingTriples[index].subject
});
const rdfslabel = n3.Util.getLiteralValue(labelresult.object);
const id = rdfslabel.replace(' ', '-').toLowerCase();
_parkings.push(new Parking(rdfslabel, parkingTriples[index].subject, id, totalspaces, datasetUrl));
}
return _parkings;
}
这样可以更容易地调试正在进行的操作。