- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
一个 model.js 文件包含这个模型:
exports.Conversations = db.sequelize.define('conversations', {
room_id: {
type: db.Sequelize.STRING
},
user_id: {
type: db.Sequelize.STRING
},
friend_id: {
type: db.Sequelize.STRING
},
}, {
timestamps: true,
createdAt:'created_at',
updatedAt:'updated_at',
deletedAt:'deleted_at',
freezeTableName: true // Model tableName will be the same as the model name
});
exports.checkRoom = function(user_id,friend_id) {
models.Conversations.findOne({ where: { $or: [{user_id: user_id , friend_id: friend_id}, {user_id: friend_id , friend_id: user_id}] }} ).then(function(conversation) {
return conversation;
});
}
SELECT
"id", "room_id", "user_id", "friend_id", "created_at", "updated_at"
FROM
"conversations" AS "conversations"
WHERE
(("conversations"."user_id" = '127' AND "conversations"."friend_id" = '124')
OR ("conversations"."user_id" = '124' AND "conversations"."friend_id" = '127'))
LIMIT 1;
var conversation = query.checkRoom(data.userId,data.friendId));
conversationId = query.checkRoom(data.userId, data.friendId).then(function(conversation) { return conversation.dataValues.id; })
最佳答案
您正在尝试使用 checkRoom()
作为同步函数,但事实并非如此(就像大多数处理 Node 中 I/O 的函数一样)。
相反,您应该返回 findOne()
返回的 promise 并在您的调用代码中处理该 promise 的解析(和拒绝):
// query.js
exports.checkRoom = function(user_id, friend_id) {
return models.Conversations.findOne({ where: { $or: [{user_id: user_id , friend_id: friend_id}, {user_id: friend_id , friend_id: user_id}] }} );
}
// calling code
query.checkRoom(data.userId, data.friendId).then(function(conversation) {
// do something with the database result
...
})
关于node.js - 使用 sequelizejs 查询返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37069440/
我用 node script.js 运行这个 sequelize 代码 const Sequelize = require('./node_modules/sequelize') const sequ
我有以下代码。这个想法是我每隔一段时间更新一个数据库行,但是如果我在此脚本运行时手动从数据库中删除该行,save() 仍然进入成功(),但该行实际上并未放回数据库. (因为 sequelize 使用
我遇到了一个异常,在任何地方都找不到解决方案。看起来像是 sequelizeJs 的 bug。 这是问题: 我有两个模型 module.exports = db.sequelize.define('u
我试图弄清楚如何将数组作为实例属性的值传递。目前,我在模型中将 dataType 设置为 STRING,并将 jQuery 字段中的值插入每个表单字段值到我从正文中解析并设置为属性的数组中,发现源。不
我正在使用 NodeJS 和 Sequelize。我有以下问题: 阅读设置表: Settings.findOne({where: { user_id: data }})
我正在使用 sequelizejs。我的数据库是 Mssql。将数据写入表时出现问题。这是我得到的错误: Unhandled rejection SequelizeDatabaseError: Con
我的模型: module.exports = function(sequelize, DataTypes){ var Podcast = sequelize.define('Podcast', {
我有一些“邀请”模型: 'use strict'; export default function (sequelize, DataTypes) { return sequelize.define
我想做一些事情,比如我在 Mongoose 中可以做的事情: Customers.find({ $or: [ {firstName: {$in:
我正在尝试诊断我的 Express 应用程序运行缓慢的原因,该应用程序使用 SequlizeJS 作为 ORM。我有一个模型与其他 2 个模型具有 2x hasMany 和 hasOne 关系: 更新
我不知道软删除是如何与 SequelizeJS 一起工作的,我也找不到任何在线文档。 我已经在 deletedAt 列中设置为 deletedAt: { type: Sequelize.DAT
如果我有一个模型用户: var User = sequelize.define('User', { name: { type: Sequelize.STRING, allowNul
是否可以在不依赖迁移的情况下使用 sequelizejs sequelize.sync() 来索引表? 如果不是,是否有任何可能的方法通过 sequelize 以编程方式进行迁移,看来文档 here
如果用户有许多项目,反之亦然,我可以使用 user.getProjects() 获取所有用户项目 - 但是有什么方法可以搜索这种关系,以便您可以翻阅结果并进行过滤(例如按项目名称)? 谢谢。 http
我想根据关联模型的参数更改模型中的值... 我不知道如何在我的预约模型中获取客户的性别... var Appointment = db.define('Appointment', { titl
所以现在我一直在使用 SequelizeJS 作为我的数据访问层,我现在最大的担忧是它试图找到一种自定义 Sequelize 模型调用的方法。 例如,假设对于 model.find() 方法,我想将缓
我正在使用 Sequelizejs 并尝试创建简单的用户验证功能 function ValidateIsUserLogged(sessionKeyInput) { var result =
我对 NodeJS 和 SequelizeJS 比较陌生,并且我正在构建的查询面临 hasOne 问题,我想知道您对此问题的看法,以找出我出错的地方以及实现此查询的正确方法。 Association
我在我的 Sequelize 应用程序中使用 PostgreSQL 和 node.js。我有两个具有一对一关系的表 - Customers 和 Users 。 UserId 表中有外键 Custome
我在 SequelizeJS 中使用关系遇到问题。 我的错误消息:(500:内部服务器错误)用户与文章无关! 文章型号: var Article = db.define('article', {
我是一名优秀的程序员,十分优秀!