IIC2513-2020-2 / syllabus

Material e información del curso
23 stars 7 forks source link

TypeError: organizations.routes is not a function #69

Closed ChayoPerez closed 3 years ago

ChayoPerez commented 3 years ago

Hola! Estoy practicando para el examen e hice una aplicación de prueba, pero tuve un problema. Hice un model llamado organization y creé su route, pero cuando el programa lee routes.js no parece encontrarlo y tira el error del título :( El resto de las páginas (como el hello) sí funcionan.

`const KoaRouter = require('koa-router'); const router = new KoaRouter();

const hello = require('./routes/hello'); const index = require('./routes/index'); const organizations = require('./routes/organizations');

router.use('/', index.routes()); router.use('/hello', hello.routes()); router.use('/organizations', organizations.routes());

module.exports = router; ` Se cae en la penúltima línea.

meretamal commented 3 years ago

Hola 😁 .

Podrías enviar el error que te está levantando?

ChayoPerez commented 3 years ago

'TypeError: organizations.routes is not a function at Object. (/mnt/c/Users/chayo/Google Drive/U/Semestre10/Web/Resumenes/AppPrueba/testApp/src/routes.js:13:44) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Module.require (internal/modules/cjs/loader.js:1025:19) at require (internal/modules/cjs/helpers.js:72:18) at Object. (/mnt/c/Users/chayo/Google Drive/U/Semestre10/Web/Resumenes/AppPrueba/testApp/src/app.js:12:16) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)`

Este es el error!

meretamal commented 3 years ago

Y podrías enviar el contenido de routes/organizations?

ChayoPerez commented 3 years ago

Perdón, no logro que github lea mi comentario como código D:

`const KoaRouter = require('koa-router'); const router = new KoaRouter();

const PERMITTED_FIELDS = [ "name", "url", ];

router.param("id", async (id, ctx, next) => { const organization = await ctx.orm.organization.findByPk(id); if (!organization) ctx.throw(404); // si no existe retorna not found ctx.state.organization = organization; return next(); });

router.get("organizations", "/organizations", async (ctx) => {

const organizations = await ctx.orm.organization.findAll({ include: "organization" });
/* organizations.forEach(org => {
  org.dataValues.cloudinaryImage = cloudinary.url(org.image);
}); */

await ctx.render("organizations/index", {
  organizations,
});

});

router.get("organizations-new", "/organizations/new", async (ctx) => { const organization = ctx.orm.organization.build(); return ctx.render("organizations/new", { organization, }); });

router.post("organizations-create", "/", async (ctx) => { const organization = ctx.orm.organization.build(ctx.request.body); try { await organization.save({ fields: PERMITTED_FIELDS }); ctx.redirect(ctx.router.url("organization", { id: organization.id })); } catch (error) { await ctx.render("organizations/new", { errors: error.errors, }); } });

router.get("organization-show", "organization/:id", async (ctx) => { const organization = await ctx.orm.organization.findByPk(id); return ctx.render("organizations/show", { organization, }); });`

meretamal commented 3 years ago

No estás exportando router.

Para insertar un bloque de código en md se usan tres backticks.

ChayoPerez commented 3 years ago

Ohhh muchas gracias! Ya me corre el programa :)