- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用风 sails 时遇到了多个问题,因为我无法理解水线 promise 及其逻辑。
我尝试了内置的 bluebird promises 和 async.waterfall
实现,但都没有成功。
简而言之,我正在为执行数据库查询的 API 编写代码,并尝试使用回调,但它从不响应。
这是我在纯粹的 promise 上所做的尝试:
changeFormation: function (request,response) {
console.log("changeFormation");
var lineupId = request.params.id;
var newFormation = request.param('formation');
var newLineUp = request.param('lineup');
console.log("Receiving" + newFormation);
if ( ["5-4-1", "5-3-2", "4-5-1", "4-4-2", "4-3-3", "3-5-2", "3-4-3"].indexOf(newFormation) === -1 ) {
console.log("No válida");
return response.send(409, "La táctica elegida no es válida");
}
LineUp.findOne({id: lineupId}).
then(function (foundLineUp) {
console.log(foundLineUp);
if (!foundLineUp)
return response.send(404);
if (! foundLineUp.formation) {
foundLineUp.formation = newFormation;
LineUp.update({id: foundLineUp.id}, foundLineUp).exec(function (err, saved) {
if (err)
return response.send(500, JSON.stringify(err));
return response.send(202, JSON.stringify(saved));
});
}
// If a formation was previously set
else if (Array.isArray(newLineUp) && newLineUp.length > 0) {
newLineUp.formation = newFormation;
LineUp.update({id: newLineUp.id}, newLineUp).exec(function (err,saved) {
if (err)
return response.send(500,JSON.stringify(err));
return response.stringify(202, JSON.stringify(saved));
});
}
console.log("Never reached");
}).
catch(function (err) {
console.log(err);
response.send(500,JSON.stringify(err));
});
},
在上面我可以在控制台中看到 “Never reached”
。为什么!?
这是我尝试使用异步模块的结果:
addPlayer: function (request,response) {
// console.log("Add player");
var lineupId = request.params.id;
var receivedPlayer = request.param('player');
var playerId = receivedPlayer.id;
var bench = receivedPlayer.bench;
var place = receivedPlayer.place;
var e, r;
async.waterfall([
function (cb) {
LineUp.findOne().where({id: lineupId}).exec(function (err, foundLineUp) {
cb(err,foundLineUp);
});},
function (lineup,cb) {
Player.findOne().where({id: playerId}).exec(function (err,foundPlayer) {
cb(err,lineup, foundPlayer);
});},
function (lineup, player, cb) {
if (!player) {
console.log("Jugador no existe");
cb(null, {status: 409, msg: "El jugador " + playerId + " no existe"});
}
if (!lineup.formation) {
console.log("No hay táctica")
cb(null, {status: 409, msg: "No se ha elegido una táctica para esta alineación"});
}
if (lineup.squadIsComplete()) {
console.log("Ya hay 15");
cb(null, {status: 409, msg: "La plantilla ya contiene el máximo de 15 jugadores"});
}
if (lineup.playerWasAdded(player.id)) {
console.log("Jugador ya en alineación")
cb(null, {status: 409, msg: "El jugador ya ha sido agregado a la alineación"});
}
if (lineup.fieldIsComplete() && !bench) {
console.log("Ya hay 11 en el campo");
cb(null, {status: 409, msg: "Ya se han agregado los 11 jugadores de campo"});
}
player.bench = bench;
player.place = place;
lineup.players.push(player);
console.log("MaxForeign " + lineup.reachesMaxForeignPlayers());
console.log("BudgetLimit " + lineup.reachesBudgetLimit());
console.log("SameTeam " + lineup.reachesMaxSameTeamLimit());
console.log("reachesMaxSameFavoriteTeamLimit " + lineup.reachesMaxSameFavoriteTeamLimit());
// If any of rule restrictions evaluates to true ...
// Using lodash _.some with out second argument which defaults to _.identity
/* if ( _.some([ lineup.reachesMaxForeignPlayers(),
lineup.reachesBudgetLimit(),
lineup.reachesMaxSameTeamLimit(),
lineup.reachesMaxSameFavoriteTeamLimit()]) ) {
return response.send(409, "La inclusión de este jugador no satisface las reglas del juego");
}*/
LineUp.update({id: playerId}, lineup).exec(function (err, saved) {
cb(err, {status: 202, msg: JSON.stringify(saved)});
});
}
],
function (err, result) {
console.log("About to respond");
if (err)
respond.send(500);
else
response.send(result.status, result.msg);
});
console.log("Never reached");
},
这不是超时,但奇怪的是它没有在应该更新文档的时候更新文档。它正在记录 “从未到达”
,然后是 “即将响应”
,但我想这是正常的。
到目前为止,我应该如何处理这一切?
最佳答案
In this above I can see in console "Never reached". Why!?
因为您将异步代码与同步代码混合在一起。如果你这样做:
function(){
console.log('init');
someAsyncMethod(function callback(){
return console.log('async done');
});
console.log('Never reached');
}
你会得到:
init
Never reached
async done
因为异步代码会在之后执行。我建议你阅读 this和 this更好地理解异步回调。
This gives not a timeout but it's strangely not updating the document when it should.
很难说这是怎么回事,因为我们不知道 LineUp
的模型定义我们不知道 lineup
的内容update
前后称呼。你确定LineUp.update()
跑了?为什么不添加 console.log()
在它的回调中看到结果?
So far, how should I handle it all?
在我看来,您离实现目标不远了。如果您分享LineUp
的模型定义和更多日志记录,我们将能够为您提供更多帮助。
关于javascript - 我应该如何处理水线和 Bluebird 的 promise 和回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949282/
我有以下模型测试 module.exports = { attributes: { a: { type: 'number' },
我有视频模型: module.exports = { attributes: { id: 'string', tags: 'array' }, } 我想找到所有带有标签的视频,
我有这些模型: // Material.js module.exports = { attributes: { name: { type: 'string',
我想知道在 SpriteKit 中创建水线的最佳方法是什么甚至在 SceneKit (如果这是一个更好的选择)? 我所说的水线是指在 2D 游戏中指示水位的动画波浪线——例如游戏“跳鱼”或“鱼出水”游
我正在使用带有 sails.js 的 mongodb 查询包含 torrent 文件标题的模型。但我无法使用“AND”条件执行水线查询。我已经尝试过多种方法,但大多数方法返回空值或根本不返回。 例如数
我的一些模型没有 id 作为主键,我需要知道给定模型的主键名称,但我找不到执行此操作的方法。我在waterline文档和源代码中进行了搜索,但我在源代码中发现的所有内容是它们正在使用attribute
假设我有 2 个模型。 用户和俱乐部。 为了简化我的问题,假设用户只有 Id 而俱乐部有 ClubId 和 UserId,因此一个俱乐部可以有许多不同的用户。 这是我的问题: 例如,当我删除 ID 为
因此,我正在使用 SailsJS 创建一个小型应用程序。我的数据库是MySQL。当我测试时,首先我创建一个“市场”记录,然后创建一个链接到“市场”的“股票”记录,在另一时刻,我检索该股票记录。 我有以
我正在开发一个包含多个(>2)表的 sails 应用程序,我需要在填充方法的帮助下加入这些表例如 Category.js 模型 attributes: { CategoryID:{
我有两个模型 - 用户和标签。用户模型的一部分 module.exports = { attributes: { tags: { collection:
使用 sails.js (0.10.5): 我不知道如何使用水线/风 sails 实现 groupBy/计数。我想要的是: SELECT customer_id, COUNT('') FROM cus
我是 Sails.js (v0.10.5) 和 Waterline ORM 的新手。我在数据库中有 3 个表:用户(id,名称),角色(id,别名)和连接表 users_roles(user_id,r
我正在将 Waterline ORM 用于sails.js。 limit和 sort使用 groupby 时不起作用,但是当您不进行任何分组时工作正常。 例如 Model.find({ gro
我正在使用 Sails.js(waterline orm)制作 API 我需要但无法完成的是从表(模型)中过滤结果,在包括嵌套字段在内的所有字段中进行搜索: 即: GET/users/?filter=
如何执行此查询? SELECT * FROM blog GROUP BY MONTH(createdAt) 我尝试过的: Blog.find() .groupBy({MONTH:'createdAt'
我想从模型中获取新 行。在给定日期之后创建的行。查询应该很简单: 其中 updatedAt>= givenDate 但它不起作用:( 我的代码: // Date from client
我正在尝试使用 Sails.js 和 Waterline.js 在现有 Mongo 数据库之上构建应用程序。问题是当我启动服务器时它说 $ sails lift error: Hook failed
我已经为我的模型之一中的字段之一设置了验证规则 maxLength,如果我的输入太长,它会被 sails.js 识别。我的问题是,如果我检查数据库,它会显示 mysql 数据库的字段长度为 255。我
我试图仅获取与我的站点模型关联的标签的序列号。这就是我正在做的事情: const site = await Site.findOne({ id: siteId }).populate('tag
我将 Waterline 与 Sails.js 结合使用。 我想要一个具有参与的用户。一个事件可以有多个参与。 参与具有状态。 我的用户类别: module.exports = { attribute
我是一名优秀的程序员,十分优秀!