Answer the question
In order to leave comments, you need to log in
Why do I get a 204 response code instead of 200, although the data should be?
I send a request to the server and in the handler I get data from the database through sequelize
, here is the fragment code:
const products = {
getAll: async () => {
let goods = await Product.findAll({raw:true})
.catch(err => console.log(`DataBase Error ${err}`))
console.log(`goods`);
console.log(goods);
return goods;
},
[nodemon] restarting due to changes...
[nodemon] starting `node --use-strict ./src/app.js`
Server listen on localhost:80
Executing (default): SELECT 1+1 AS result
Executing (default): SELECT "id", "title", "description", "price", "pathImage" FROM "products" AS "product";
app:db Connection has been established successfully. +0ms
goods
[
{
id: 1,
title: 'newItem',
description: 'testItem',
price: 100,
pathImage: null
}
]
res =
appServiceData.js:10 {data: "", status: 204, statusText: "No Content", headers: {…}, config: {…}, …}
appServiceData.js:11 res.data =
class AppServiceData {
async getResourse(url) {
const res = await axios.get(url);
console.log('res = ');
console.log(res);
console.log('res.data = ');
console.log(res.data);
return res.data;
};
async getProducts() {
let res = await this.getResourse('http://localhost/api/products');
return res;
};
};
res =
appServiceData.js:10 {data: "goods", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
appServiceData.js:11 res.data =
appServiceData.js:12 goods
const path = require('path');
const Koa = require('koa');
const serve = require('koa-static');
const err = require('./middleware/error');
const router = require('./middleware/api/routes');
const debug = require('debug')('app');
const send = require('koa-send');
const staticDir = path.resolve(__dirname, '..', '..', 'public');
const app = new Koa();
app.use(err);
app.use(serve(staticDir));
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async ctx => await send(ctx, 'index.html', { root: staticDir }));
app.listen(3000, () => {
console.log('Server listen on localhost:80');
});
const router = new Router();
const koaBody = convert(KoaBody());
//**products endpoints */
.post('/api/products', async ctx => {
ctx.body = await products.addProduct();
})
.get('/api/products', async ctx => {
ctx.body = await products.getAll();
})
Answer the question
In order to leave comments, you need to log in
callback that executes koa, and the fact that you use it is clear only from your comment, it receives the context argument as input
async (ctx) => {
ctx.body = 'твой ответ'
}
data: "", status: 204 data: "goods", status: 200
if you explicitly send some object in return, then everything is fine
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question