【问题标题】:Sequelize throwing "Invalid value { searchTerm: 'Kaizer fc', sportType: '2' }" on Postman续集在 Postman 上抛出“无效值 { searchTerm: 'Kaizer fc', sportType: '2' }”
【发布时间】:2022-01-25 15:05:26
【问题描述】:

我正在尝试创建一个搜索团队名称和过滤到运动类型的序列化查询,但是当我在 Postman 上测试 JSON 正文时出现错误并且它没有返回数据。 JSON 正文与其要返回但不匹配的数据相匹配。

邮递员测试

{
   "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 - 模型文件夹

'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 - 控制器

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;

【问题讨论】:

  • 显示req.body的内容
  • req.body 的内容是 { "name": "Kaizer fc", "sport_type_id": "2" }
  • 作为一个对象?还是作为字符串?
  • 它是一个带有 post 方法的对象

标签: javascript node.js express sequelize.js


【解决方案1】:

search 函数有两个参数:searchTermsportType 并且您将整个 req.body 作为第一个参数传递,这就是为什么它成为 searchTerm 的值并且您收到关于整个值的错误来自续集。
只需从 req.body 中提取两个 props 或定义 search 并将 props 作为对象传递:

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

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

【讨论】:

  • { "message": "命名参数 \":search_name\" 在给定对象中没有值。" }
  • 检查searchTermindise search函数
  • 你选择了什么解决方案?
  • 第一个解决方案就像魔术一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2020-01-15
相关资源
最近更新 更多