I found a demo in the koa-router official docs.
router.get(
'/users/:id',
(ctx, next) => {
//something
},
async ctx => {
//I can't use await here
}
);
But why can't I use async and await in the second middleware? I always got Not found response by default.
Related
I've been looking through the docs for a day now, and still can't figure out this simple question: what is the #slack/bolt ^2.3.0 method that is used to listen for a Modal submission?
const slackbot = new App({ token, signingSecret });
slackbot.shortcut(
"open_modal",
async ({ ack, body, client, context, payload }) => {
await ack();
const result = await client.views.open({ ... });
}
);
slackbot.METHOD_HERE(ID_HERE,
async ({ ack }) => {
ack();
// handle modal submitted data here...
}
);
What values take the place of METHOD_HERE and ID_HERE in the code above?
I am able to open a modal without problem via the global shortcuts menu; but can't seem to figure out how to capture user's submission of the modal.
For example, this never captures any submissions:
slackbot.view("open_modal", async ({ ack }) => {
ack();
// do things here...
});
You need to use the callback_id used when you create the Modal view:
slackbot.shortcut(
"open_modal",
async ({ ack, body, client, context, payload }) => {
await ack();
const result = await client.views.open({
trigger_id: body.trigger_id,
view: {
type: "modal",
callback_id: "YOUR_CALLBACK_ID", // <= listen for this ID
title: {
type: "plain_text",
text: "Modal Title",
},
blocks: [ ... ],
},
});
}
);
Then, to listen to submissions on the above Modal, use this:
app.view('YOUR_CALLBACK_ID', optionalMiddleWareFunction, async ({ payload }) => {
const submittedValues = payload.view.state.values
// do stuff with submittedValues
});
callback_id is the ID that you defined when creating modal view. You can see an example here.
You can read the corresponding official documentation here.
I have gone through Bot v4 documentation and felt very surprised and happy by looking into Events of ActivityHandler class. But I am unable to trigger some of them in my bot through Facebook channel.
I have tried below code in my class which extends ActivityHandler class. Below mentioned events unable to trigger. In fact, I dont know how to trigger these events.
onEvent
onMembersRemoved
onReactionsAdded
onReactionsRemoved
onMessageReaction
onTokenResponseEvent
onUnrecognizedActivityType
Below is the code Snippet.
class DialogBot extends ActivityHandler {
constructor(conversationState, userState, dialog, logger,conversationReferences) {
super();
}
this.onEvent(async (context, next) => {
console.log('=========== from onEvent funcion from class=====');
await next();
});
this.onMembersRemoved(async (context, next) => {
console.log('----dialog bot.js ----onMembersRemoved---- ');
await next();
});
this.onMessageReaction(async (context, next) => {
console.log('----dialog bot.js ----onMessageReaction---- ');
await next();
});
this.onReactionsAdded(async (context, next) => {
console.log('----dialog bot.js ----onReactionsAdded---- ');
await next();
});
this.onReactionsRemoved(async (context, next) => {
console.log('----dialog bot.js ----onReactionsRemoved---- ');
await next();
});
this.onTokenResponseEvent(async (context, next) => {
console.log('----dialog bot.js ----onTokenResponseEvent---- ');
await next();
}) ;
this.onUnrecognizedActivityType(async (context, next) => {
console.log('----dialog bot.js ----onUnrecognizedActivityType---- ');
await next();
});
}
Please suggest the way to utilize these powerful events through facebook channel in bot framework v4 node sdk.
I look forward for your response. thanks.
Not all channels support all events.
onMembersRemoved -> Not supported by Facebook
onMessageReaction -> Not supported by Facebook
onReactionsAdded -> Not supported by Facebook
onReactionsRemoved -> No supported by Facebook
onTokenResponseEvent -> Which token?
This is a reference chart that show which channel supports which event activity.
[Error][1]
Hi Team,
Whenever I am receiving the error return from laravel the nuxt.js project displays the error on the page instead the HTML/Forms. How can i handle this.
Here is my php code
return response()->json([
'errors' => [
'email' => ['Sorry we cant find you with those details.'],
],
], 422);
Javascript
async submit() {
await this.$auth.loginWith("local", {
data: this.form
})
In your JavaScript you need to wrap your await promise inside a try catch block. Here's a fix for your JS.
try {
await this.$auth.loginWith("local", {
data: this.form
})
} catch (e) {
return;
}
This is an old question at this point, but I thought I'd post the full code since I was pretty stumped and didn't find many great answers out there:
async handleSubmit() {
try {
const authResponse = await this.$auth.loginWith('local', {
data: this.formData
});
const { status, data } = authResponse;
if (status === 200)
this.createFlashAlert({ 'success': 'Login successful' });
} catch (error) {
if (error.response.status === 422)
this.createFlashAlert(error.response.data);
}
}
So the checklist:
Wrap the login call in a try/catch if you're using async await syntax (be sure to make it an async function i.e. async handleSubmit.
in the catch block, use the error.response object, this is an axios thing. With this you'll be able to access the response status and data.
If you log just the error object, it's not obvious that you can access the response within that error which is what had me stumped.
So I have this function in Koa, that basically checks if a user can access a specific route.
exports.requireRole = async role =>
async (ctx, next) => {
const { user } = ctx.state.user;
try {
const foundUser = await User.findById(user.id);
// If the user couldn't be found, return an error
if (!foundUser) {
ctx.status = 404;
ctx.body = { errors: [{ error: ERRORS.USER_NOT_FOUND }] };
} else {
// Otherwise, continue checking role
if (getRole(user.role) >= getRole(role)) {
await next();
}
ctx.status = 403;
ctx.body = { errors: [{ error: ERRORS.NO_PERMISSION }] };
}
} catch (err) {
ctx.throw(500, err);
}
};
And I want to use it as a middleware:
router.delete('/:id', combine([jwtAuth, requireRole(ROLES.ADMIN)]), deleteUser);
But then I get an error saying:
middleware must be a function not object
This happens only when I try to pass an argument into it.
What am I doing wrong here?
The issue you are having is due to the fact that Promises are objects, and async functions return Promises. You need to change your initial function to be as follows:
exports.requireRole = role =>
instead of
exports.requireRole = async role =>
I was going over middleware myself, and ran into this issue as well.
Your middleware looks fine, what is combine?
Also, since you are using koa-router you don't need it.
router.delete('/:id', jwtAuth, requireRole(ROLES.ADMIN), deleteUser);
I'm trying to build a simple REST API with Koa. For this, I am using koa-router. I have two problems:
Whenever I try to add parameters to my POST-Method in mainRouter.ts like ":id", Postman shows a "not found". My request: http://localhost:3000/posttest?id=200
I cannot get the parameters with "ctx.params". I also can't find anything about it on the koajs-page, but I do see examples like this everywhere?!
This is my app:
app.ts
import * as Koa from 'koa';
import * as mainRouter from './routing/mainRouter';
const app: Koa = new Koa();
app
.use(mainRouter.routes())
.use(mainRouter.allowedMethods());
app.listen(3000);
mainRouter.ts
import * as Router from 'koa-router';
const router: Router = new Router();
router
.get('/', async (ctx, next) => {
ctx.body = 'hello world';
});
router
.post('/posttest/:id', async (ctx, next) => {
ctx.body = ctx.params.id;
});
export = router;
If I change the POST-method to this, then I get "200":
router
.post('/posttest', async (ctx, next) => {
ctx.body = ctx.query.id;
});
If you're using a query string in your request like this:
http://localhost:3000/posttest?id=200
Then your route handler should be using ctx.query, not ctx.params:
router.post('/posttest', async (ctx, next) => {
console.log(ctx.query.id); // 200
});
You should only use ctx.params when you want to send requests like this:
http://localhost:3000/posttest/200
In which case you would write the route handler like so:
router.post('/posttest/:id', async (ctx, next) => {
console.log(ctx.params.id); // 200
});