node.js - 全局 Knex.raw 已弃用,使用 knex.raw (链接一个初始化的 knex 对象)
问题描述
我正在使用knex
并且pg
我想从 node.js 调用 postgreSQL 函数,最好使用 knex。我用过
const knex = require("knex");
exports.getAll = async (companyID) => {
console.log("from here", companyID);
const getAll = await databaseProvider
.knex("attendance")
.select(knex.raw("select * from get_allFromCompany('?')",[companyID]));
return databaseProvider.executeQuery(getAll).then((result) => {
return result.rows;
});
但我收到错误:
global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)
(node:25) UnhandledPromiseRejectionWarning: Error: Unable to acquire a connection
at Client_PG.acquireConnection (/app/node_modules/knex/lib/client.js:340:13)
at Runner.ensureConnection (/app/node_modules/knex/lib/runner.js:264:8)
at Runner.run (/app/node_modules/knex/lib/runner.js:26:12)
at Builder.Target.then (/app/node_modules/knex/lib/interface.js:19:43)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:25) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
};
我还尝试了其他几种黑客方法,但我无法这样做。我的 sql 查询是
SELECT * FROM get_allFromCompany('1');
有人可以帮我吗?
解决方案
您需要使用您正在使用的方言初始化 knex 实例,然后创建一个与 knex 绑定的查询,然后才能使用 DB 驱动程序执行它。
const knex = require("knex")({client: 'pg'});
exports.getAll = async (companyID) => {
console.log("from here", companyID);
const getAllQuery = databaseProvider
.knex("attendance")
.select(knex.raw("select * from get_allFromCompany('?')",[companyID])).toSQL().toNative();
return databaseProvider.executeQuery(getAllQuery .sql, getAllQuery .bindings).then((result) => {
return result.rows;
});
推荐阅读
- javascript - 对象搜索键值并删除其中的第一个和最后一个字符
- curl - 重定向后的 cURL PUT
- javascript - 如何等待 http 请求完成?Javascript
- wordpress - 为什么 Wordpress 创建的图像缩略图(文件大小)比原始图像大?
- python-3.x - 如何更改照片的位置?
- java - 使用 jackson-dataformat-csv 获取 csv 的标头
- blender - 为什么我的动画在渲染时看起来完全不同?
- javascript - 信息没有通过 Mongoose 传递给数组,无法找出原因
- python - 将文件从 django 上传到服务器中的特定目录?
- php - 如何加入这 3 个表并使我的 Select 查询正常工作?