Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler function cannot have void return type #345

Closed
AlexSteinerETH opened this issue Feb 17, 2022 · 12 comments
Closed

Handler function cannot have void return type #345

AlexSteinerETH opened this issue Feb 17, 2022 · 12 comments
Labels
question Issue can be closed by providing information

Comments

@AlexSteinerETH
Copy link

AlexSteinerETH commented Feb 17, 2022

Hi when i run my script i got this error:

/usr/local/include/crow/middleware.h: In instantiation of ‘typename std::enable_if<crow::black_magic::is_callable<F, crow::request, Args ...>::value>::type crow::detail::wrapped_handler_call(crow::request&, crow::response&, const F&, Args&& ...) [with F = main()::<lambda(const crow::request&)>; Args = {}; typename std::enable_if<crow::black_magic::is_callable<F, crow::request, Args ...>::value>::type = void]’:
/usr/local/include/crow/routing.h:583:47:   required from ‘void crow::TaggedRule<Args>::operator()(Func&&) [with Func = main()::<lambda(const crow::request&)>; Args = {}]’
Run.cpp:55:6:   required from here
/usr/local/include/crow/middleware.h:238:27: error: static assertion failed: Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable
  238 |             static_assert(!std::is_same<void, decltype(f(std::declval<crow::request>(), std::declval<Args>()...))>::value,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/crow/middleware.h:241:25: error: invalid use of void expression
  241 |             res = crow::response(f(req, std::forward<Args>(args)...));
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My code:

#include <iostream>
#include <ctime>
#include <list>


#include <crow.h>

#include "Wallets/Wallets.hpp"
#include "Headers/json.hpp"

using namespace std;
using namespace crow;

Wallets wallets;


int main()
{
    SimpleApp server;

    CROW_ROUTE(server, "/")([]() {
        mustache::context ctx;
        auto main_page = mustache::load("index.html");
        return main_page.render();
    });

    CROW_ROUTE(server, "/wallet").methods("GET"_method)([](const crow::request& req) {
        auto main_page = mustache::load("add_wallet.html");
        return main_page.render();
    });

    CROW_ROUTE(server, "/create-wallet").methods("POST"_method)([](const crow::request& req) {
        cout << endl;

        const Wallet& newWallet = wallets.CreateWallet();

        cout << endl;

        crow::json::wvalue x({{}});
        x["Hash"] = newWallet.wallet_hash;
        x["Phrase"] = newWallet.secret_phrase;

        auto main_page = mustache::load("successfully_wallet_created.html");
        return main_page.render(x);
    });

    CROW_ROUTE(server, "/login").methods("GET"_method)([](const crow::request& req) {
        auto main_page = mustache::load("login_wallet.html");
        return main_page.render();
    });

    
    CROW_ROUTE(server, "/login-wallet").methods("POST"_method)([](const crow::request& req) {
        cout << req.url_params.get("hash") << endl;
    });


    server.port(8080).multithreaded().run();
}

I USE UBUNTU AND I RUN THE CODE WITH :

 #Delete the previous run
rm Run

#Run the server
g++ -std=c++11 Run.cpp -o Run -L /usr/lib/ -lboost_system -lboost_thread -lpthread -ggdb -lz -D CROW_ENABLE_DEBUG $(pkg-config --cflags --libs libmongocxx)

#clear

./Run


Middleware.h

@The-EDev The-EDev added the question Issue can be closed by providing information label Feb 17, 2022
@The-EDev
Copy link
Member

your last route takes only a request as argument (meaning the route has to end with return) but you're not returning anything. if you just want to return an OK message to the client, you should do return response();

@AlexSteinerETH
Copy link
Author

i just want to return the data of an html input

@The-EDev
Copy link
Member

okay, you should add something like return req.url_params.get("hash")

@AlexSteinerETH
Copy link
Author

Have this error: (2022-02-17 14:54:57) [ERROR ] An uncaught exception occurred: basic_string::_M_construct null not valid (2022-02-17 14:54:57) [INFO ] Response: 0x7f51540022b0 /login-wallet 500 0

@The-EDev
Copy link
Member

could it be that the hash parameter doesn't exist?

@AlexSteinerETH
Copy link
Author

I read the documentation, and i used in a wrong way url_params.get(), i just nead to extract data from an html form.

@The-EDev
Copy link
Member

html form would probably use multipart, currently it's a little inconvenient to do so but an issue already exists for that (#312) and I'm hoping to implement a PR for it soon.

Let me know if there's anything else related to this you need help with.

@The-EDev The-EDev changed the title Middleware.h Handler function cannot have void return type Feb 17, 2022
@AlexSteinerETH
Copy link
Author

Solved, thank you!

I have another question, must I open another issue?

@The-EDev
Copy link
Member

Please go ahead, no need to open another issue

@AlexSteinerETH
Copy link
Author

Ok, does or will, crow implement ejs?

@The-EDev
Copy link
Member

The-EDev commented Feb 17, 2022

I had to read up on what EJS was, and it seems to be a templating engine built for nodeJS and uses JS syntax. Crow doesn't implement it, it instead uses its own implementation of Mustache to render templates.

From what I currently know about EJS, I'm not sure there's a point to implementing it when Crow has a templating engine.

@AlexSteinerETH
Copy link
Author

Ok thats all thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

2 participants