React-native: RCT_EXPORT_METHOD that takes no parameters and returns a promise throws error

Created on 1 Dec 2017  Â·  10Comments  Â·  Source: facebook/react-native

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

yes

Environment

Environment:
OS: macOS Sierra 10.12.6
Node: 8.9.0
Yarn: 1.3.2
npm: 5.5.1
Watchman: 4.9.0
Xcode: Xcode 9.1 Build version 9B55
Android Studio: 3.0 AI-171.4408382

Packages: (wanted => installed)
react: 16.0.0-beta.5 => 16.0.0-beta.5
react-native: 0.49.3 => 0.49.3

Target Platform: iOS (10.3)

Steps to Reproduce

(Write your steps here:)

  1. Create a native module that returns a promise

TestNativeModule.h

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>

@interface TestNativeModule : NSObject <RCTBridgeModule>
@end

TestNativeModule.m

@implementation TestNativeModule
RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(get: findEventsWithResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolve("test");
}
@end
  1. Call it from JS:
const promise = NativeModules.CallToAdventureNative.get();
  1. See the error:
CallToAdventureNative.get was called with 0 arguments but expects 1 arguments. If you haven't changed this method yourself, this usually means that your versions of the native code and JavaScript code are out of sync. Updating both should make this error go away.

Note: It works if you pass Anything to the .get function.

Expected Behavior

It would call the function.

Actual Behavior

The error posted above.

Reproducible Demo

It's a native module, can't repro the web

Ran Commands Locked

Most helpful comment

Try:

RCT_EXPORT_METHOD(get:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolve("test");
}

All 10 comments

What are you talking about

On Nov 30, 2017, at 8:48 PM, JPTeasdale notifications@github.com wrote:

Is this a bug report?

Yes

Have you read the Contributing Guidelines https://facebook.github.io/react-native/docs/contributing.html?

yes

Environment

Environment:
OS: macOS Sierra 10.12.6
Node: 8.9.0
Yarn: 1.3.2
npm: 5.5.1
Watchman: 4.9.0
Xcode: Xcode 9.1 Build version 9B55
Android Studio: 3.0 AI-171.4408382

Packages: (wanted => installed)
react: 16.0.0-beta.5 => 16.0.0-beta.5
react-native: 0.49.3 => 0.49.3

Target Platform: iOS (10.3)

Steps to Reproduce

(Write your steps here:)

Create a native module that returns a promise
TestNativeModule.h

import

import

@interface TestNativeModule : NSObject
@end
TestNativeModule.m

@implementation TestNativeModule
RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(get: findEventsWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
resolve("test");
}
@end
Call it from JS:
const promise = NativeModules.CallToAdventureNative.get();
See the error:
CallToAdventureNative.get was called with 0 arguments but expects 1 arguments. If you haven't changed this method yourself, this usually means that your versions of the native code and JavaScript code are out of sync. Updating both should make this error go away.
Note: It works if you pass Anything to the .get function.

Expected Behavior

It would call the function.

Actual Behavior

The error posted above.

Reproducible Demo

It's a native module, can't repro the web

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/facebook/react-native/issues/17037, or mute the thread https://github.com/notifications/unsubscribe-auth/ATjBYY2wbFr95EoBzkAMulkOdI59QFynks5s71rlgaJpZM4QxwA9.

Try:

RCT_EXPORT_METHOD(get:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolve("test");
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

FWIW this is still broken as of 0.53, although I believe you can get around it via the hacky solution stated above:

In the Objective-C

RCT_EXPORT_METHOD(someMethodName: (NSString *)blank
        resolve(RCTProniseResolveBlock)resolve
        reject(RCTPromiseRejectBlock)reject) {
    // Do something
    resolve(nil);
}

Then in JS

SomeModule.someMethodName("");

Like I said, super hacky.

@anderskev Have you tried:

RCT_EXPORT_METHOD(someMethodName:(RCTPromiseResolveBlock)resolve
                  reject:(RCTPromiseRejectBlock)reject) {
  // Do something
  resolve(nil);
}

?
This is the expected Objective-C signature for a method receiving only resolve & reject arguments.

Something went wrong executing that command, @hramos could you take a look?

Thanks for posting this! It looks like you may not be using the latest version of React Native, v0.53.0, released on January 2018. Can you make sure this issue can still be reproduced in the latest version?

I am going to close this, but please feel free to open a new issue if you are able to confirm that this is still a problem in v0.53.0 or newer.

How to Contribute • What to Expect from Maintainers

the function

RCT_EXPORT_METHOD(get: findEventsWithResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolve("test");
}

should be:

RCT_EXPORT_METHOD(get: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  resolve("test");
}

I'm just checking in on this. I'm also running into this error. Is there a better solution than that empty string hack?

@cmwall The issue here was a incorrect syntax, there's no need for an empty string. What's your native method signature?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

despairblue picture despairblue  Â·  3Comments

axelg12 picture axelg12  Â·  3Comments

josev55 picture josev55  Â·  3Comments

vikeri picture vikeri  Â·  3Comments

jlongster picture jlongster  Â·  3Comments