我使用rxjs
中的React Native
。我打电话给Observable.fromPromise(storage.load({key: key})).map(() => value);
显示错误。
我的rxjs版本:
"rxjs": "^6.5.3",
"rxjs-compat": "^6.5.3",
我有三个步骤。
步骤1:
rxInit().flatMap(() => {
console.log('I can not see the console log');
return rxInit()
}).subscribe(() => {
console.log('I can not see the console log');
// some code...
})
Step2 rxInit():
import { Observable } from 'rxjs';
rxInit() {
console.log('I can see the console log')
return StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
console.log('I can't not see the console log')
if (deviceuuid == null) {
return StorageService.shared.set('deviceuuid', this.deviceuuid);
} else {
return Observable.of(this.deviceuuid);
}
}).do((deviceuuid) => {
// some code...
})
}
有关get()的Step3:
import { Observable } from 'rxjs';
import Storage from 'react-native-storage';
import AsyncStorage from '@react-native-community/async-storage';
let storage = new Storage({
size: 1000,
storageBackend: AsyncStorage,
defaultExpires: null,
})
export default class StorageService {
set(key, value) {
console.log('StorageService set');
return Observable.fromPromise(storage.save({ key: key, data: value })).map(() => value);
}
get(key) {
console.log('It is a ', storage.load({key: key})); // It is a Promise
return Observable.fromPromise(storage.load({key: key})).map(() => value);
}
remove(key) {
return Observable.fromPromise(storage.remove({key: key})).catch(() => Observable.of(null))
}
}
StorageService.shared = new StorageService();
我在寻找答案,有人说这是因为rxjs高于6.0,所以我尝试使用
import { from } from 'rxjs';
return from(storage.load({key: key})).map(() => value);
窗台显示
TypeError: _rxjs.from.fromPromise is not a function
或
_rxjs.from().map is not a function.
任何帮助将不胜感激。
我了解评论中已经有一些提示,但是我会提供一个答案,以便为答案提供更多背景信息。
对于RxJS 6,与RxJS运算符(例如map和switchMap)一起使用的做法是使用pipe
实用程序,而不是对运算符进行点链接。您可能会详细了解here上的更改。
例如,以您的示例为基础,如果您想同时使用map
和filter
运算符,而不是执行类似操作,则>]
from(storage.load({key: key})).map(() => value).filter(value => value)
RxJS 6将要求您执行此操作:
from(storage.load({key: key}))
.pipe(
map(() => value),
filter(value => value),
.subscribe(res => {
console.log(res);
// do the rest here
})