admin管理员组文章数量:1332345
I'm looking into converting some basic JS to Kotlin but I'm stuck on the new
keyword. I'm not sure how to convert the following JS to Kotlin
var FCM = require('fcm-node');
var fcm = new FCM('YOURSERVERKEYHERE');
var message = { ... };
fcm.send(message, function(err, response){ ... }
I tried
fun sendTestPush() {
val FCM = require("fcm-push")
val fcm = new FCM("YOURSERVERKEYHERE")
val data = Data("Title", "Message")
val message = Message("registration_id", data)
fcm.send(message)
}
data class Message(val to: String, val data: Data)
data class Data(val title: String, val message: String)
I get the pile error Unresolved reference: new
as Kotlin doesn't have it.
Without the 'new' I get the expected error Attempting to TypeError: Cannot read property 'send' of undefined
Any idea to get around this problem?
Edit: FCM class is the npm package
I'm looking into converting some basic JS to Kotlin but I'm stuck on the new
keyword. I'm not sure how to convert the following JS to Kotlin
var FCM = require('fcm-node');
var fcm = new FCM('YOURSERVERKEYHERE');
var message = { ... };
fcm.send(message, function(err, response){ ... }
I tried
fun sendTestPush() {
val FCM = require("fcm-push")
val fcm = new FCM("YOURSERVERKEYHERE")
val data = Data("Title", "Message")
val message = Message("registration_id", data)
fcm.send(message)
}
data class Message(val to: String, val data: Data)
data class Data(val title: String, val message: String)
I get the pile error Unresolved reference: new
as Kotlin doesn't have it.
Without the 'new' I get the expected error Attempting to TypeError: Cannot read property 'send' of undefined
Any idea to get around this problem?
Edit: FCM class is the npm package https://www.npmjs./package/fcm-push
Share Improve this question edited Dec 2, 2018 at 8:15 Allan Veloso 6,4091 gold badge41 silver badges36 bronze badges asked Sep 16, 2017 at 6:00 mononzmononz 1,7051 gold badge13 silver badges17 bronze badges 2- What's the FCM class? – fweigl Commented Sep 16, 2017 at 8:55
-
1
you'll have to use
js()
to wrap your JavaScript library. see kotlinlang/docs/tutorials/javascript/… – Claies Commented Sep 16, 2017 at 13:25
3 Answers
Reset to default 3Sorry but your answer you marked as correct, is in fact incorrect. I have to say this, since someone who is looking for the right answer might find it and write incorrect code. Generally, you shouldn't call the require
function from Kotlin directly. Rather, you should use @JsModule
together with external
declarations. In your particular case it would be something like this:
@JsModule("fcm-push")
external class FCM(serverKey: String) {
fun send(message: Any?, callback: (err: Any?, response: Any?) -> Unit)
fun send(message: Any?): Promise<Any>
}
val serverKey = "YOURSERVERKEYHERE"
val fcm = FCM(serverKey)
//...
fcm.send(message)
Also, you should pass monjs
to the moduleKind
piler flag. See the corresponding documentation page for a full description.
The require function in Kotlin is not the same as the require in NodeJS which is probably used in your JS code.
Whatever your FCM class is, just instantiate it without the new
keyword.
Thanks to a hint from @Claies I managed get it to work using the js(...)
wrap.~~~
val FCM = require("fcm-push")
val serverKey = "YOURSERVERKEYHERE"
val fcm = js("new FCM(serverKey)")
...
fcm.send(message) // now works
I'm not sure I'm totally happy with the writing pure js inside a string in kotlin so I hope there is a better way that I've missed.
Edit: The above works, but its not ideal, refer to accepted answer for better implementation
本文标签: javascriptHow to convert JS code to Kotlin without the 39new39 keywordStack Overflow
版权声明:本文标题:javascript - How to convert JS code to Kotlin without the 'new' keyword - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327027a2453932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论