我正在致电有关User的信息,并希望包含UserMeetingsReviews,除非属性包含空字符串:complement === ''
如果我使用include: [ where: { complement: '' } ]
,它会包含所有补码为空字符串的记录,没有问题。为了只获取没有空字符串的记录,我尝试过:
include: [ where: { 'complement': {[Op.ne]: '""'} }, ]
include: [ where: { 'complement != "" '}, ]
include: [ where: [ '`GroupMeetingsReviews`.`complement` != ""' ],]
所有人都抛出错误。
这不会引发错误,但是会包含空字符串的记录:
where: {[Op.not]: [ { complement: '""' }, ] },
在Rails中相当于where.not(complement: '')
-如何在nodejs中包含带有否定where查询的记录?
这里是我的完整档案:
'use strict'
let users = {};
const { BlockedUsers, LanguagesUsers, ProSettings, Users, UserReviews, GroupMeetingsReviews, GroupMeetings, Languages } = require('../models');
let models = require("../models/index");
let Sequelize = require('sequelize');
let Op = Sequelize.Op;
let sequelizeDB = require('../modules/Sequelize');
users.getProfileByUserId = (req, res) => {
models.Users.findOne({
where: {id: req.params.userid},
include: [
{
model: models.UserReviews,
as: 'complements',
},
{
model: models.GroupMeetingsReviews,
where: {
'complement': {[Op.ne]: ''}
},
as: 'groupComplements',
include: [
{
model: models.Users,
as: 'reviewee',
attributes: ['id', 'avatar_file_name', 'first_name']
},
{
model: models.Users,
as: 'reviewer',
attributes: ['id', 'avatar_file_name', 'first_name']
},
{
model: models.GroupMeetings,
as: 'group_meeting',
},
]
},
],
}).then(user => {
models.sequelize
.query(
"SELECT languages_users.level, languages_users.language_id, languages.native, languages.locale, languages.flag, languages.language FROM languages_users JOIN languages ON languages_users.language_id = languages.id WHERE languages_users.user_id = ? ORDER BY level DESC",
{
replacements: [req.params.userid],
type: models.PushToken.sequelize.QueryTypes.SELECT
}
)
.then(languages => {
user["encrypted_password"] = null;
res.status(200).json({ status: 200, data: { user, languages } });
});
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
});
};
有以下建议
model: models.GroupMeetingsReviews,
as: 'groupComplements',
where: {
complement: {
[Op.ne]: '',
},
},
这些是服务器日志:
2018-09-27T15:04:57.197162 + 00:00 app [web.1]:执行(默认):SELECT“ Users”。“ id”,“ Users”。“ email”,“ Users”。“ “ encrypted_password”,“ Users”。“ first_name”,“ Users”。“ last_name”,“ Users”。“ bio”,“ Users”。“ location”,“ Users”。“ avatar_file_name”,“ Users”。“国籍” ,“ Users”。“状态”,“ Users”。“ lat”,“ Users”。“ lng”,“ complements”。“ id” AS“ complements.id”,“ complements”。“审阅者” AS“ complements。 “ reviewer”,“ complements”。“ reviewee” AS“ complements.reviewee”,“ complements”。“ meeting_id” AS“ complements.meeting_id”,“ complements”。“ notes” AS“ complements.notes”,“ complements”。“ created_at“ AS” complements.created_at“,” complements“。” updated_at“ AS” complements.updated_at“,” groupComplements“。” id“ AS” groupComplements.id“,” groupComplements“。” reviewer_id“ AS” groupComplements.reviewer_id“ ,“ groupComplements”。“ reviewee_id” AS“ groupComplements.reviewee_id”,“ groupComplements”。“ group_meeting_id” AS“ groupComplements.group_meeting_id”,“ groupComplements”。“ complement” AS“ groupComplements.complement” ,“ groupComplements”。“ reviewed” AS“ groupComplements.reviewed”,“ groupComplements”。“ created_at” AS“ groupComplements.created_at”,“ groupComplements”。“ updated_at” AS“ groupComplements.updated_at”,“ groupComplements-> reviewee”。 “ id” AS“ groupComplements.reviewee.id”,“ groupComplements-> reviewee”。“ avatar_file_name” AS“ groupComplements.reviewee.avatar_file_name”,“ groupComplements-> reviewee”。“ first_name” AS“ groupComplements.reviewee.first_name”, “ groupComplements-> reviewer”。“ id” AS“ groupComplements.reviewer.id”,“ groupComplements-> reviewer”。“ avatar_file_name” AS“ groupComplements.reviewer.avatar_file_name”,“ groupComplements-> reviewer”。“ first_name” AS“ groupComplements.reviewer.first_name“,” groupComplements-> group_meeting“。” id“ AS” groupComplements.group_meeting.id“,” groupComplements-> group_meeting“。” lang_one_id“ AS” groupComplements.group_meeting.lang_one_id“,” groupComplements-> group “。” lang_two_id“ AS” groupComplements.group_meeting.lang_two_id“,” groupComplements-> group_meet ing“。” location_name“ AS” groupComplements.group_meeting.location_name“,” groupComplements-> group_meeting“。” created_at“ AS” groupComplements.group_meeting.created_at“,” groupComplements-> group_meeting“。” updated_at“ AS” groupComplements.group_meet “ updatedat”来自“ public”。“ users”作为“ Users”左外部联接“ public”。“ user_reviews” AS“ complements”在“ Users”上。“ id” =“ complements”。“ reviewee” INNER JOIN“ public”。 “ group_meetings_reviews” AS“ groupComplements”在“ Users”上。“ id” =“ groupComplements”。“ reviewee_id” AND“ groupComplements”。“ complement”!=“左外联接” public“。” users“ AS” groupComplements-> reviewee“ ON” groupComplements“。” reviewee_id“ =” groupComplements-> reviewee“。” id“左外加入” public“。”用户“ AS” groupComplements-> reviewer“ ON” groupComplements“。” reviewer_id“ =” groupComplements- > reviewer“。” id“左外联接” public“。” group_meetings“ AS” groupComplements-> group_meeting“ ON” groupComplements“。” group_meeting_id“ =” groupComplements-> group_meeting“。” id“其中“用户” s“。” id“ ='32';
2018-09-27T15:04:57.224689 + 00:00 app [web.1]:未处理的拒绝TypeError:无法将属性'encrypted_password'设置为null 2018-09-27T15:04:57.224692 + 00:00 app [web.1]:在models.sequelize.query.then.languages(/app/controllers/Users.js:104:40) 2018-09-27T15:04:57.224694 + 00:00 app [web.1]:在tryCatcher(/app/node_modules/bluebird/js/release/util.js:16:23) 2018-09-27T15:04:57.084036 + 00:00 app [web.1]:解码令牌,{id:32,创建:1537994368165,iat:1537994368} 2018-09-27T15:04:57.224698 + 00:00 app [web.1]:在Promise._settlePromiseFromHandler(/app/node_modules/bluebird/js/release/promise.js:512:31) 2018-09-27T15:04:57.224702 + 00:00 app [web.1]:在Promise._settlePromise(/app/node_modules/bluebird/js/release/promise.js:569:18) 2018-09-27T15:04:57.217311 + 00:00 app [web.1]:执行(默认):选择languages_users.level,angulars_users.language_id,angulars.native,angulars.locale,angulars.flag,language.language FROM Languages_users在Languages_users.language_id = languages.id上加入语言WHERE Languages_users.user_id ='32'在DESC级别上排序 2018-09-27T15:04:57.224705 + 00:00 app [web.1]:在Promise._settlePromises(/app/node_modules/bluebird/js/release/promise.js:694:18) 2018-09-27T15:04:57.224707 + 00:00 app [web.1]:在_drainQueueStep(/app/node_modules/bluebird/js/release/async.js:138:12) 2018-09-27T15:04:57.224709 + 00:00 app [web.1]:在_drainQueue(/app/node_modules/bluebird/js/release/async.js:131:9) 2018-09-27T15:04:57.224703 + 00:00 app [web.1]:在Promise._settlePromise0(/app/node_modules/bluebird/js/release/promise.js:614:10) 2018-09-27T15:04:57.224718 + 00:00 app [web.1]:在processImmediate [作为_immediateCallback](timers.js:745:5) 2018-09-27T15:04:57.224710 + 00:00 app [web.1]:位于Async._drainQueues(/app/node_modules/bluebird/js/release/async.js:147:5) 2018-09-27T15:04:57.224714 + 00:00 app [web.1]:在runCallback(timers.js:810:20) 2018-09-27T15:04:57.224712 + 00:00 app [web.1]:在Instant.Async.drainQueues(/app/node_modules/bluebird/js/release/async.js:17:14) 2018-09-27T15:04:57.224716 + 00:00 app [web.1]:在tryOnImmediate(timers.js:768:5)
答案 0 :(得分:0)
要获得complement
不是''
或NULL
的结果,请使用以下语法:
model: models.GroupMeetingsReviews,
as: 'groupComplements',
where: {
complement: {
[Op.ne]: '',
[Op.ne]: null,
},
},
required: true,
您还可以将logging: true
添加到查询中,以通过console.log()
查看生成的SQL,以进行其他调试。