Home > Net >  Sequelize throwing "Invalid value { searchTerm: 'Kaizer fc', sportType: '2'
Sequelize throwing "Invalid value { searchTerm: 'Kaizer fc', sportType: '2'

Time:01-27

Im trying to create a sequalized query that Searches For a Team Name & Filter To Sport Type but when I test the JSON body on Postman I get an error and its not returning the data. The JSON body matches the data that its meant to return but its not matching up.

Test on Postman

{
   "searchTerm": "Kaizer fc",
   "sportType": "2"
}

teams.js

const Teams = require('../models').teams;
const {sequelize, QueryTypes } = require('sequelize');
const db = require('../models')

const search = (searchTerm, sportType) => {
  return new Promise(async (resolve, reject) => {
    try {     
      
        console.log("Testing");

        const teams = await db.sequelize.query(
          `SELECT teams.name, institutions.name, addresses.line1, addresses.line2, addresses.country 
           FROM teams
           INNER JOIN institutions
           ON teams.institution_id = institutions.id
           INNER JOIN addresses
           ON institutions.address_id = addresses.id
           WHERE teams.name like :search_name and teams.sport_type_id LIKE :sport_type`,
          {
            replacements: { search_name: searchTerm, sport_type: sportType },
            type: QueryTypes.SELECT
          }
        );

        return resolve(teams);
      } catch (err) {
        return reject(err)
      }
  })
}

teams.js - Models Folder

'use strict';
module.exports = (sequelize, DataTypes) => {
    const teams = sequelize.define('teams', {
        name: { type: DataTypes.STRING, allowNull: false },
        media_id: { type: DataTypes.STRING, allowNull: true },
        state_id: { type: DataTypes.INTEGER, allowNull: true },
        status_id: { type: DataTypes.INTEGER, allowNull: true },
        sport_type_id: { type: DataTypes.INTEGER, allowNull: false },
        created_by: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'users', key: 'id' } },
        modified_by: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'users', key: 'id' } },
        primary_user_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'users', key: 'id' } },
        institution_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'institutions', key: 'id' } },
    }, {
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        indexes: [
            {
                unique: true,
                fields: ['id']
            }
        ],
    });
    return teams;
};

search.js - Controllers

const helper = require('../utils/helper');
const teamsService = require('../services/teams');

const search = async (req, res) => {

    try {
        const data = await teamsService.search(req.body);

        console.log("TESTING");
        console.log(data);
        
        return res.send(data);

    } catch (err) {
        return helper.handleError(err, req, res);
    }
}

module.exports = search;

CodePudding user response:

search function has two arguments: searchTerm and sportType and you pass the whole req.body as a first argument so that's why it becomes a value for searchTerm and you got this error about the whole value from Sequelize.
Just extract both props from req.body OR define search with props passed by as an object:

const { searchTerm, sportType } = req.body;
const data = await teamsService.search(searchTerm, sportType);

OR

const data = await teamsService.search(req.body);
...
const search = ({ searchTerm, sportType }) => {
  return new Promise(async (resolve, reject) => {
  •  Tags:  
  • Related