Plugin POST route 404...But logic is executed...HUH?

I have been developing a plugin, and I have been using the import-content plugin as a way to figure out how the Strapi system works.

My aim is to use the google sheets api to both create and update data. I have set up a endpoint for the plugin that receives a POST request with the ID of the google sheet.

The endpoint is defined in config/routes.json

      "method": "POST",
      "path": "/",
      "handler": "sheets.fetch",
      "config": {
        "policies": []

I then have a controller at controllers/sheets.js

module.exports = {
   fetch: async (ctx) => {...}

There is a lot going on inside this function. And I have been trying to end the process with

ctx.send({message: 'ok'})

However, I keep getting an error message both in the browser and console. The weird part is that all my code is executed and the new content is created in the database. What is going on here?

System Information
  • Strapi Version: 3.3.4
  • Operating System: Windows
  • Database: postgres
  • Node Version: 14.15.0
  • NPM Version:
  • Yarn Version:

What error exactly do you get in browser and console? Just the 404?
Is it returned instantly? Or after your code is executed?

Just the 404 in the terminal.

This shows up in the browser. It seems to be instantly.

Can you replace all your custom code inside the fetch controller with:
ctx.send({message: 'ok'});

Does it still return 404?

Its fine if I replace the code. 200.

My actual code includes oauth for the google sheets api - reads a json file then calls the api, processes the data, etc.

There is a lot of callbacks - do you think trying to flatten things out to with async would help?


Flattening out callbackville worked somewhat. As long as the token is valid it returns 200. My only issue is the authorization.

Right now if the token is missing or not valid, I am outputting a url to the terminal. I then click that url, go through the google oauth, and get redirected back to the admin homepage. I then copy and paste the code url param from the browser back into the terminal - this triggers the token to be saved to a json file. This can’t be the best way to do this…are we able to make our own route handlers?

Could be a cool plugin, it writes the ID back to the google sheet so it can be used for updates later…ecommerce

You can use puppetry to automate that process.

That means some of your functions return something that causes 404. I would write all that custom login into a custom service with try-catch instead of writing all the logic in the controller.

Koa defaults all responses to code 404, I would suggest you return a JSON object instead of using the ctx.message / ctx.code.

something like:

return {
  message: 'your message here'

This will automatically make koa use the 200 code and return the json you set. If you need to send an error I’d suggest using something like:

return ctx.badRequest('some message') 

This comes from the boom middleware, which will automatically set the code properly, you can find all the options here:

Thanks for the info.
I’m having fun figuring Strapi out.

Its mostly Koa, if you want to gather a fairly strong knowledge of Strapi; understanding some of the tech we use like Koa (and its many packages), Bookshelf/Knex, Mongoose, etc is a great place to start.

1 Like