gpt4 book ai didi

node.js - 包括模型,除非属性为空 - Node Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:39:22 24 4
gpt4 key购买 nike

我正在调用有关用户的信息并希望包含 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]: Executing (default): SELECT "Users"."id", "Users"."email", "Users"."encrypted_password", "Users"."first_name", "Users"."last_name", "Users"."bio", "Users"."location", "Users"."avatar_file_name", "Users"."nationality", "Users"."status", "Users"."lat", "Users"."lng", "complements"."id" AS "complements.id", "complements"."reviewer" 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_meeting"."lang_two_id" AS "groupComplements.group_meeting.lang_two_id", "groupComplements->group_meeting"."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_meeting.updated_at" FROM "public"."users" AS "Users" LEFT OUTER JOIN "public"."user_reviews" AS "complements" ON "Users"."id" = "complements"."reviewee" INNER JOIN "public"."group_meetings_reviews" AS "groupComplements" ON "Users"."id" = "groupComplements"."reviewee_id" AND "groupComplements"."complement" != '' LEFT OUTER JOIN "public"."users" AS "groupComplements->reviewee" ON "groupComplements"."reviewee_id" = "groupComplements->reviewee"."id" LEFT OUTER JOIN "public"."users" AS "groupComplements->reviewer" ON "groupComplements"."reviewer_id" = "groupComplements->reviewer"."id" LEFT OUTER JOIN "public"."group_meetings" AS "groupComplements->group_meeting" ON "groupComplements"."group_meeting_id" = "groupComplements->group_meeting"."id" WHERE "Users"."id" = '32';

2018-09-27T15:04:57.224689+00:00 app[web.1]: Unhandled rejection TypeError: Cannot set property 'encrypted_password' of null 2018-09-27T15:04:57.224692+00:00 app[web.1]: at models.sequelize.query.then.languages (/app/controllers/Users.js:104:40) 2018-09-27T15:04:57.224694+00:00 app[web.1]: at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23) 2018-09-27T15:04:57.084036+00:00 app[web.1]: decoded token, { id: 32, created: 1537994368165, iat: 1537994368 } 2018-09-27T15:04:57.224698+00:00 app[web.1]: at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:512:31) 2018-09-27T15:04:57.224702+00:00 app[web.1]: at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:569:18) 2018-09-27T15:04:57.217311+00:00 app[web.1]: Executing (default): 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 = '32' ORDER BY level DESC 2018-09-27T15:04:57.224705+00:00 app[web.1]: at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:694:18) 2018-09-27T15:04:57.224707+00:00 app[web.1]: at _drainQueueStep (/app/node_modules/bluebird/js/release/async.js:138:12) 2018-09-27T15:04:57.224709+00:00 app[web.1]: at _drainQueue (/app/node_modules/bluebird/js/release/async.js:131:9) 2018-09-27T15:04:57.224703+00:00 app[web.1]: at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:614:10) 2018-09-27T15:04:57.224718+00:00 app[web.1]: at processImmediate [as _immediateCallback] (timers.js:745:5) 2018-09-27T15:04:57.224710+00:00 app[web.1]: at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:147:5) 2018-09-27T15:04:57.224714+00:00 app[web.1]: at runCallback (timers.js:810:20) 2018-09-27T15:04:57.224712+00:00 app[web.1]: at Immediate.Async.drainQueues (/app/node_modules/bluebird/js/release/async.js:17:14) 2018-09-27T15:04:57.224716+00:00 app[web.1]: at tryOnImmediate (timers.js:768:5)

最佳答案

要获得 complement 不是 ''NULL 的结果,请使用以下语法:

model: models.GroupMeetingsReviews,
as: 'groupComplements',
where: {
complement: {
[Op.ne]: '',
[Op.ne]: null,
},
},
required: true,

您还可以将 logging: true 添加到您的查询中,以通过 console.log() 查看生成的 SQL 以进行额外的调试。

关于node.js - 包括模型,除非属性为空 - Node Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com