2

I want to receive data (simply as a string) from the client-web side to update the database, but it's a bit lock for me now, so first write the data to a file in the drive with using System.IO.File.WriteAllText(@"my-file.txt")


I got that error

error FS9001: Method name not found in JavaScript compilation: (receive : System.Object -> unit)

Can you tell me where I did wrong and fix it?


s952163
  • 6,276
  • 4
  • 23
  • 47

2 Answers2

2

WebSharper's client side can call methods that are themselves in [<JavaScript>] scope or marked [<Remote>] for remote calls. The error message is not mentioning the second option, but that is what you need here (same as the sample function DoSomething has it too).

You will also need to make the remote function to not send over an obj but a string. Remote function arguments are deserialized based on type information and cannot be obj. For example in client code, use Server.receive rvInput.Value. (rvInput is a reactive variable for which .Value contains current value)

Note that if you want to return a value to the server, the remote function must be an async. Here, just for logging, returning unit works too, but then you have no way on the server to determine if the logging was successful. By returning an async<unit>, you can catch errors in the client code if you want to guard against connection or server errors. Again, the sample code in the template gives some guidance.

Jand
  • 391
  • 1
  • 6
  • I'm making everything as simple as possible as you can see in the following code [Remoting](https://gist.github.com/tungandcun/09ea271409f53f90a1f74d712010bdcb) but I still get that error FS9001: Type not found in JavaScript compilation: SampleWebsite.receive – ATU stackOverflow Feb 17 '18 at 02:54
  • Your `module receive` is not marked with `[]` – Jand Mar 19 '18 at 16:50
2

(Cross posting from http://forums.websharper.com/topic/84579)

Here is what your server-side function should look like:

[<Remote>]
let Receive (input: string) =      
    async {
        System.IO.File.WriteAllText(@"D:/myDatabase.txt", "Server received data: " + input)
    }

and to call it from Client.fs, you need:

...
        button [
            on.click (fun _ _ ->
                async {
                    do! Server.Receive rvInput.Value
                } |> Async.Start
            )
        ] [text "Receive"]
...
Adam Granicz
  • 121
  • 2