我正在尝试启动我的React Native Redis模块并运行, 但出现以下错误。当我尝试引用任何功能
TypeError: undefined is not an object (evaluating '_reactNativeRedispubsub.RedisClient.redisConnect')
RedisClient.js
/**
* This exposes our Android code into our RN project
*/
import { NativeModules } from "react-native";
module.exports = NativeModules.RedisClient;
通过Redison Jar进行实际Redis调用的文件 RedisClient.java
package com.redispubsub;
import android.util.Log;
import android.widget.Toast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.redisson.client.RedisConnectionException;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
public class RedisClient extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private RedissonClient mredissonClient;
public RedisClient(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RedisClient";
}
@ReactMethod
public void redisConnect(String ipPort) {
//Redisson client setup to be done in background thread
try {
Config config = new Config();
config.useSingleServer().setAddress(ipPort); //IPAddress of Redis and port
mredissonClient = Redisson.create(config);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@ReactMethod
public void subscribe(String channelName) {
if (mredissonClient != null) {
RTopic subscribeTopic = mredissonClient.getTopic(channelName, StringCodec.INSTANCE); //Channel to listen and Codec.
subscribeTopic.addListener(new MessageListener < String > () {
@Override
public void onMessage(String channel, String msg) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(channel, msg);
}
});
} else {
}
}
@ReactMethod
public long publish(String ChannelName, String msg) {
long clientsReceivedMessage = 0;
if (mredissonClient != null) {
//Redisson publish
RTopic topic = mredissonClient.getTopic(ChannelName, StringCodec.INSTANCE);
try {
clientsReceivedMessage = topic.publish(msg); //Publishing lat lon to Coros
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
}
return clientsReceivedMessage;
}
}
将方法公开给Java脚本 RedisCleintPackage.java
package com.redispubsub;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
public class RedisClientPackage implements ReactPackage {
@Override
public List < NativeModule > createNativeModules(ReactApplicationContext reactContext) {
List < NativeModule > modules = new ArrayList < > ();
modules.add(new RedisClient(reactContext));
return modules;
}
public List < Class << ? extends JavaScriptModule >> createJSModules() {
return Arrays.asList();
}
@Override
public List < ViewManager > createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
此_reactNativeRedispubsub.RedisClient.redisConnect是否表示其未正确调用对象?
我很困惑为什么它不使用RedisClient.java中存在的redisConnect
这整个代码在我运行MainActivity的我的react-native android项目中正常工作,但是当我将其发布到npm并进行npm安装时,它的工作正常
答案 0 :(得分:0)
我明白了
以下更改需要在 redisclient.js
/ ** *这会将我们的Android代码公开到我们的RN项目中 * /
import { NativeModules } from "react-native";
const { RedisClient } = NativeModules;
export {
RedisClient
}
答案 1 :(得分:0)
我使用这样的东西:
import {NativeModules} from 'react-native';
module.exports = NativeModules.RedisClient;