Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
No puedo crear una publicación en mi base de datos, el problema es que devuelve la identificación del usuario como nula

Mi solicitud de cartero:

 { "content": "my content", "likes": 0 }

Respuesta en cartero:

 { null }

Aquí está mi código:

 'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Post extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { // define association here models.Post.belongsTo(models.User, { foreignKey: { allowNull: false, name: "userId" } }) } }; Post.init({ content: DataTypes.STRING, comments: DataTypes.STRING, attachment: DataTypes.STRING, likes: DataTypes.INTEGER }, { sequelize, modelName: 'Post', }); return Post; };

controladores:

 module.exports.createPost = async (req, res, next) => { const token = req.cookies.jwt; const decoded = jwtAuth.verify(token, process.env.TOKEN_SECRET); console.log(decoded); const userIdFound = await User.findByPk(decoded.id);

No puedo obtener la identificación de usuario a pesar de la solicitud, devuelve un valor nulo:

 try { const { body } = req; console.log(body); await User.findOne({ where: { id: userIdFound } }) .then((userFound) => { //Create a Post if User Id is valid if (userFound) { const newPost = Post.create( { ...body, userId } ).then(() => res.status(201).json(newPost)) .catch(err => res.status(404).json('Unauthorizated request !')) }})

Los errores:

 } catch (err) { throw new Error(err); } }

muchas gracias por tu ayuda (todavía soy un principiante, seguramente habrá algunos errores).

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

No entiendo de dónde "empuja" el userIdFound en su cláusula where en la consulta.

Pero creo que puedes intentar insertar el ID de valor en una const y usarlo en el lugar. Suponiendo que el nombre del campo de identificación en su HTML que envía la publicación o la solicitud de obtención es "id", puede insertarlo así:

 const userIdFound = req.body.id; await User.findOne({ where: { id: userIdFound } }) //Some code for the logic

Puedes ver más en la documentación aquí

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!