javascript - 为什么承诺的“成功”会给我一个函数不存在的错误?
问题描述
我有一些看起来像这样的代码:
var db = require("../db.js");
module.exports = {
init: function(app) {
// run searchin our database
app.post("/api/search", function (req, res) {
// generate search sql
try {
var sql_tables = "select attraction.id, title, src from attraction LEFT JOIN attraction_picture ON attraction_picture.attraction_id = attraction.id";
var sql_group_by = " group by attraction.id";
var sql_order_by = " ORDER BY ";
var params = {};
// order those with pictures first, ALWAYS
sql_order_by += " if (attraction_picture.id IS NOT NULL, 0, 1), 0";
// order based on those with higher ratings first
sql_order_by += " + attraction.stars ";
// give slight preference to those with more ratings (popularity)
sql_order_by += " + if (reviews > 5000, 5, reviews/1000) ";
// apply preferences for each different category independently
if (typeof(req.body.sess_user.filters) != "undefined") {
for (var i in req.body.sess_user.filters) {
i = parseInt(i); // make sure its an integer
if (!req.body.sess_user.filters[i]) continue;
// join to figure out if this attraction matches this filter
var thistable = "filter_" + i;
sql_tables += " LEFT JOIN attraction_filter " + thistable + " ON " + thistable +
".attraction_id = attraction.id AND " + thistable + ".filter_id=:filter_id_" + i + " ";
params["filter_id_" + i] = i;
// if it does match, give points or take away points based on the star.
var adjustment_if_matches = (req.body.sess_user.filters[i]) - 3 * 3;
sql_order_by += " + if(" + thistable + ".attraction_id IS NOT NULL, :filter_adjustment_" + i + ", 0) ";
params["filter_adjustment_" + i] = adjustment_if_matches;
}
}
// per page
sql_order_by += " DESC LIMIT :start, :per_page";
params.start = 0;
if (typeof(req.body.start) != "undefined") {
params.start = req.body.start;
}
console.log(req.body.start);
params.per_page = 24;
// execute sql and return results
app.db.q(sql_tables + sql_group_by + sql_order_by, params).success(function(results) {
res.json(results);
});
} catch (err) {
console.log("error running search: ");
console.log(err);
res.json([]); // @TODO - better error handling
}
});
这段代码的作用并不是很重要,但本质上它会通过一个 mysql 数据库并选择其中的某些元素。
在这一行:
// execute sql and return results
app.db.q(sql_tables + sql_group_by + sql_order_by, params).success(function(results) {
res.json(results);
我收到此错误:
$ sudo nodemon app.js
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
Listening on port 80
0
passing raw query replacements as the 4th argument to sequelize.query is deprecated. Please use options.replacements instead
error running search:
TypeError: app.db.q(...).success is not a function
at /home/tex/studygit/Planning-App/api/attractions.js:54:68
at Layer.handle [as handle_request] (/home/tex/studygit/Planning-App/node_modules/express/lib/router/layer.js:76:5)
at next (/home/tex/studygit/Planning-App/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/home/tex/studygit/Planning-App/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/home/tex/studygit/Planning-App/node_modules/express/lib/router/layer.js:76:5)
at /home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:227:24
at Function.proto.process_params (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:305:12)
at /home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:221:12
at Function.match_layer (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:288:3)
at next (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:182:10)
at /home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:184:16
at Function.match_layer (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:288:3)
at next (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:182:10)
at /home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:184:16
at Function.match_layer (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:288:3)
at next (/home/tex/studygit/Planning-App/node_modules/express/lib/router/index.js:182:10)
我很困惑为什么会发生这个错误。此特定功能在我的db.js
文件中定义,该文件位于该文件的父目录中。所以它看起来像这样:
- root
- directory
. attractions.js
. db.js
我收到错误的文件是景点.js。正如我所说,该函数的定义位于 db.js 中,如下所示:
// bootstrap the db wrapper
var Sequelize = require('sequelize');
var conf = require("./conf");
// Database Wrapper Initialization
try {
if (conf.db.type == "mysql") {
// connect to database
var db = new Sequelize("mysql://" + conf.db.username + ":"+conf.db.password+
"@" + conf.db.host + ":" + conf.db.port + "/" + conf.db.database);
} else {
console.log("PROBLEM - UNKNOWN DB TYPE: " + conf.db.type);
process.exit(1);
}
} catch (err) {
console.log("PROBLEM - connecting to db: ");
console.log(err);
process.exit(1);
}
// simplified database querying function
db.q = function(query, replace) {
return this.query(query, null, {raw: true}, replace);
};
module.exports = db; // return our db connection
我只是在制作 Sequelize 模块中提供的简化查询功能。sequelize 模块状态:
public query(sql: string, options: Object): Promise 在数据库上执行查询,有可能绕过所有的 sequelize goodness。
默认情况下,该函数将返回两个参数:一个结果数组和一个元数据对象,包含受影响的行数等。
如果您正在运行不需要元数据的查询类型,例如 SELECT 查询,则可以传入查询类型以对结果进行 sequelize 格式:
sequelize.query('SELECT...').then(([results, metadata]) => {
// Raw query - use then plus array spread
});
sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }).then(results => {
// SELECT query - use then
})
如果查询是一个承诺,那为什么我不能使用db.q(...).success
?为什么说该功能不存在?我在这里做错了什么我没有看到吗?我对 Javascript 有点陌生,所以很可能......
任何帮助,将不胜感激。谢谢你。
解决方案
推荐阅读
- sql - 如何检查存储在 SQL Server DB 表中的 word 文件中某些字符串的可用性?
- python - 多个分隔符的嵌套列表中的文本到 CSV(跳过第一次出现)
- excel - 如何修复 Excel VBA 中的“下标超出范围”错误:在表的更改事件中获取目标单元格的行号
- php - 使用 Doctrine Query Builder 转换 SQL 查询
- linux - 按数字顺序对制表符分隔文件中的行进行排序
- django - 在 django 中执行 PostgreSql 查询
- c# - 设置 TypeBuilder.CreateType() 的位置?
- javascript - 当用户按下 instagram/facebook 上的后退按钮时捕获关闭事件
- c++ - 文件中的每十个数字是偶数还是奇数
- java - 使用 paho MQTT 代理订阅时出错