无法使用SEQUELIZE Node.js检索具有特定电话号码的用户数据

编程入门 行业动态 更新时间:2024-10-08 03:28:24

无法使用SEQUELIZE Node.js检索具有特定<a href=https://www.elefans.com/category/jswz/34/1765017.html style=电话号码的用户数据"/>

无法使用SEQUELIZE Node.js检索具有特定电话号码的用户数据

我已经在Node.js中使用SEQUELIZE创建了两个关联的表(Account,AccountCLI)。帐户表与AccountCLI表有很多关联。帐户表包含一列(用户ID,eppusername,用户名,vendorsparameter,projectid)。 AccountCLI表具有列(电话号码,用户ID(ForeignKey))。当用户输入电话号码时,它将返回相应的用户数据。在我的代码中,它返回所有用户数据,而不是特定用户的电话号码。您可以在下面找到代码。请给点建议吗?

表条目显示为:

  'use strict';
module.exports = (sequelize, DataTypes) => {
  const Account = sequelize.define('Account', {
    epprojectname: DataTypes.STRING,
    username: DataTypes.STRING,
    projectid: DataTypes.STRING,
    vendorparameters: DataTypes.STRING,
    credentials: DataTypes.STRING
  }, {});
  Account.associate = function(models) {
    Account.hasMany(models.AccountCLI, {
      as: 'accountcli',
      foreignKey: 'userid'
    });
  };
  return Account;
};

'use strict';
module.exports = (sequelize, DataTypes) => {
  const AccountCLI = sequelize.define('AccountCLI', {
    phonenumber: DataTypes.STRING,
    userid: DataTypes.INTEGER
  }, {});
  AccountCLI.associate = function(models) {
    AccountCLI.belongsTo(models.Account, {
      as: "account",
      foreignKey: 'userid'   
     });
  };
  return AccountCLI;
};

输入电话号码值时检索所有用户数据的代码(假设是检索特定的用户数据)显示为(这是建议之后的代码):

        // find account with specific number 
exports.findSpecificUser = async (req, res) => {
var whereStatement = {};
if(req.body.phonenumber)
whereStatement['$accountcli.phonenumber$'] = {$like: '%' + req.body.phonenumber + '%'};
  Account.findAll({  include: { model: AccountCLI, as: "accountcli", where: whereStatement } })
    .then((data) => {
      res.send(data);
    })
    .catch((err) => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving account with the specific phonenumber.",
      });
    });
};

这是我使用POSTMAN检索到的JSON文件。在这里,我放置了(phonenumber,3334),它想检索(“ epprojectname”:“ DFSumitayayabot”)用户数据,但是它同时检索了两个用户信息。

[
    {
        "id": 68,
        "epprojectname": "DFSumitayayabot",
        "username": "Sumit",
        "projectid": "ayayabot",
        "vendorparameters": "newparameters",
        "credentials": "1589956379476-tslint.json",
        "createdAt": "2020-05-19T15:23:00.440Z",
        "updatedAt": "2020-05-20T06:36:05.903Z",
        "accountcli": [
            {
                "id": 227,
                "phonenumber": "33344",
                "userid": 68,
                "createdAt": "2020-05-20T06:36:05.997Z",
                "updatedAt": "2020-05-20T06:36:05.997Z"
            },
            {
                "id": 228,
                "phonenumber": " 447467",
                "userid": 68,
                "createdAt": "2020-05-20T06:36:05.997Z",
                "updatedAt": "2020-05-20T06:36:05.997Z"
            }
        ]
    },
    {
        "id": 67,
        "epprojectname": "DFZeyadavayachatbot",
        "username": "Zeyad",
        "projectid": "avayachatbot",
        "vendorparameters": "{\"synth_speech_cfg\": { \"voice\": { \"name\": \"en-AU-Wavenet-C\"}}}",
        "credentials": "1589958578216-AppointmentType.json",
        "createdAt": "2020-05-19T15:17:43.399Z",
        "updatedAt": "2020-05-20T07:09:38.228Z",
        "accountcli": [
            {
                "id": 249,
                "phonenumber": "44433",
                "userid": 67,
                "createdAt": "2020-05-20T07:09:38.332Z",
                "updatedAt": "2020-05-20T07:09:38.332Z"
            },
            {
                "id": 250,
                "phonenumber": " 5566",
                "userid": 67,
                "createdAt": "2020-05-20T07:09:38.332Z",
                "updatedAt": "2020-05-20T07:09:38.332Z"
            }
        ]
    }
]
回答如下:

phonenumber字段在AccountCLI模型中,但是您在Account查询中的字段中添加了条件。尝试这样的事情:

if(req.body.phonenumber)
  whereStatement['$accountcli.phonenumber$'] = {$like: '%' + req.body.phonenumber + '%'};

更多推荐

无法使用SEQUELIZE Node.js检索具有特定电话号码的用户数据

本文发布于:2024-05-07 21:46:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电话号码   数据   用户   SEQUELIZE   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!