如何从查询创建一个 Observable?
How to create an Observable from query?
如何从电子中的异步 LinvoDB 数据库查询中获取 angular 2 中的 rxjs-observable?
在主进程中:
exports.load = function(parameters){
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
return doc;
});
};
在渲染器进程中,我使用 electron.remote 访问函数:
getElements(parameters) {
Observable.of(elementController.load(parameters))
.subscribe((x) => console.log(x)); // outputs 'undefined'
}
除此之外,我尝试了多种其他方法,例如将可观察对象声明为变量并合并来自 db 的结果,将查询设置为 live(),通过 ipc 获取数据。
我做错了什么?
我无法对其进行测试,但可以假设如下:
exports.load = function(parameters, subject){
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
// fire your subject here .. !
subject && subject.next && subject.next(doc) && subject.complete();
return doc;
});
};
getElements(parameters) {
const subj = new Subject<any /* or your type.. */>();
elementController.load(parameters, subj);
// return your subject here or subscribe to it..
subj.subscribe(doc => console.log(doc));
}
您可以将整个加载函数包装成返回一个 Observable:
exports.load = function(parameters){
return Observable.create(observer => {
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
observer.next(doc);
observer.complete();
});
});
};
然后在您的 getElements 函数中:
getElements(parameters) {
elementController.load(parameters)
.subscribe((x) => console.log(x));
}
希望这对你有用!
如何从电子中的异步 LinvoDB 数据库查询中获取 angular 2 中的 rxjs-observable?
在主进程中:
exports.load = function(parameters){
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
return doc;
});
};
在渲染器进程中,我使用 electron.remote 访问函数:
getElements(parameters) {
Observable.of(elementController.load(parameters))
.subscribe((x) => console.log(x)); // outputs 'undefined'
}
除此之外,我尝试了多种其他方法,例如将可观察对象声明为变量并合并来自 db 的结果,将查询设置为 live(),通过 ipc 获取数据。 我做错了什么?
我无法对其进行测试,但可以假设如下:
exports.load = function(parameters, subject){
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
// fire your subject here .. !
subject && subject.next && subject.next(doc) && subject.complete();
return doc;
});
};
getElements(parameters) {
const subj = new Subject<any /* or your type.. */>();
elementController.load(parameters, subj);
// return your subject here or subscribe to it..
subj.subscribe(doc => console.log(doc));
}
您可以将整个加载函数包装成返回一个 Observable:
exports.load = function(parameters){
return Observable.create(observer => {
var l = parseInt(parameters.l);
var t = parseInt(parameters.t);
var r = parseInt(parameters.r);
var b = parseInt(parameters.b);
return cmelement.find({
$or: [
{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
]
}).filter(function(x){ return x !== undefined })
.exec(function (err, doc) {
console.log(doc); // outputs the correct data
observer.next(doc);
observer.complete();
});
});
};
然后在您的 getElements 函数中:
getElements(parameters) {
elementController.load(parameters)
.subscribe((x) => console.log(x));
}
希望这对你有用!