Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 续写别名_Node.js_Sequelize.js - Fatal编程技术网

Node.js 续写别名

Node.js 续写别名,node.js,sequelize.js,Node.js,Sequelize.js,我正在使用sequelize,我有一个带有两个外键的模型 app.schemas.messengers.belongsTo(app.schemas.users, { foreignKey: 'id_user_to', as: 'to' }); app.schemas.messengers.belongsTo(app.schemas.users, { foreignKey: 'id_user_from', as: 'from' }); 查询结果必须返回该特定用户

我正在使用sequelize,我有一个带有两个外键的模型

app.schemas.messengers.belongsTo(app.schemas.users, {
    foreignKey: 'id_user_to',
    as: 'to'
});

app.schemas.messengers.belongsTo(app.schemas.users, {
    foreignKey: 'id_user_from',
    as: 'from'
});
查询结果必须返回该特定用户的所有消息 这是查询的代码

 return Users.findAll({
     attributes: ['uuid', 'id', 'profile_pic', 'firstname', 'lastname', 'online'],
            where: whereUser,
            include: [{
                model: Messengers,
                as: 'from',
                // as: 'to',
                where: whereMessenger,
                $or: [{
                        id_user_to: user.id,
                    },
                    {
                        id_user_from: user.id

                    }
                ],
                order: [
                    ['createdAt', 'ASC'],
                ],
            }]
        })
但是只返回写我的用户的消息,而不是我写的用户的消息。
有没有办法让我可以在sequelize的as属性上添加两个别名,或者有其他方法可以这样做?

您必须包含两次,例如

   ...
   include: [
   {
      model: Messengers,
      as: 'from'                
      /* other stuff */
   },
   {
      model: Messengers,
      as: 'to'                
      /* other stuff */
   }
  ],
  ...

此外,您的别名可能会有问题,因为“to”和“from”是保留字。我建议改为msgTo和msgFrom…

在哪里保留
到?“这是续集问题还是其他问题?”brad在SQL中保留。例如,
SELECT*FROM myTable As FROM
不起作用。。。。