Javascript knex的select出现意外行为
对于下面的代码,我得到的结果有时是数组,有时是对象。我想接收数组,即使它是空的Javascript knex的select出现意外行为,javascript,sql,postgresql,knex.js,Javascript,Sql,Postgresql,Knex.js,对于下面的代码,我得到的结果有时是数组,有时是对象。我想接收数组,即使它是空的 export const GetByPId = async (userId, pId) => knex('table1').where({ userId, pId }).select( 'userId', 'pId', 'id', 'created', 'updated', ); 在我的业务对象中,我等待响应 static async LoadByPId(userId, pId) {
export const GetByPId = async (userId, pId) => knex('table1').where({ userId, pId }).select(
'userId',
'pId',
'id',
'created',
'updated',
);
在我的业务对象中,我等待响应
static async LoadByPId(userId, pId) {
const data = await GetByPId(userId, pId);
console.log(`result ${JSON.stringify(data)}`);
}
一旦它回来
[{userId:1,id:1…-我想要这个
下次它回来的时候
{userId:1,id:1…-不想要这个
发生了什么事?如何让它始终返回数组
更新1
现在它只返回一个结果
更新2
情况每况愈下
现在我的其他基本功能不能正常工作。Knex只在第一组参数上工作,其他所有参数都失败
例如,如果快速服务器重新启动并发送一个userId:1和pId:1请求,它就会工作。如果我使用相同的参数重复相同的请求,它就会工作。但是如果我将参数ie userId或pId更改为另一个有效集,它就会失败。在尝试任何其他参数之前,我必须重新启动快速服务器。我在我的应用程序和邮递员
我的express代码如下所示
router.post('/list', auth, async (req, res) => {
try {
const biz= await BizObj.LoadByPId(req.user.id, req.body.pId);
res.json(biz);
} catch (ex) {
console.log(ex);
res.status(400).json('Unauthorized');
}
});
更新4
如果我的knex配置有问题
development: {
client: 'postgresql',
connection: {
database: 'somedb',
user: 'SOMEUSER',
password: '',
timezone: 'UTC',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
更新5
只有一页代码缺少express设置
在pg db/SomeObj中
id userId
1 1
2 2
3 2
代码示例
import knex from 'knex';
import express from 'express';
const config = {
development: {
client: 'pg',
connection: {
database: 'somedb',
user: 'SOMEUSER',
password: '',
timezone: 'UTC',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
};
const knexed = knex(config.development);
const SQL = knexed('SomeObj');
const GetAll = async userId => SQL.where({ userId }).select(
'id',
'userId',
);
const GetById = async (userId, id) => SQL.where({ userId, id }).first(
'id',
'userId',
);
class SomeObj {
constructor(data, userId) {
this.userId = userId;
this.id = data.id;
}
static async LoadAll(userId) {
const data = await GetAll(userId);
if (!data || data.length === 0) return null;
return data.map(r => new SomeObj(r, userId));
}
static async Load(userId, id) {
const data = await GetById(userId, id);
if (!data) return null;
return new SomeObj(data, userId);
}
}
const router = express.Router();
router.post('/list', async (req, res) => {
try {
const res1 = await SomeObj.LoadAll(req.body.id); // works and returns array
const res2 = await SomeObj.Load(req.body.id, 2); // fails and returns undefined
res.json({ res1, res2 });
} catch (ex) {
res.status(401).json(ex);
}
});
无法运行第二个查询。不知道是否缺少关闭连接的简单方法
更新6
我发誓knex搞乱了我。每次我尝试一些东西并返回以确认更改是由于我的新输入引起的,都会有不同的响应。现在,res1和res2都返回第一个请求的正确结果,但第二个请求失败
更新7
Runkit示例:
它对第一个请求运行,但对express server上的所有其他请求都失败
更新8
有关更多详细信息,请参阅。谢谢Mikael
knex('table1')
.where({ userId, pId })
.select('userId', 'pId', 'id', 'created', 'updated')
应该始终返回一个结果数组。您正在做示例中未显示的其他错误
示例代码:
对更新7的答复
Knex查询生成器是可变的,因此在重新使用它们时需要克隆
很好的例子,从中很容易发现问题
应该始终返回一个结果数组。您正在做示例中未显示的其他错误
示例代码:
对更新7的答复
Knex查询生成器是可变的,因此在重新使用它们时需要克隆
很好的例子,从这一点很容易发现问题,看起来它在多个记录的情况下返回一个数组,在单个记录的情况下返回一个对象只是想一想:@Vatsal有一次,它返回了一个包含单个对象的数组。如果我无法解决它,那将是我的解决办法。我不明白为什么它不一致。尝试创建一个包含初始化和所有内容的单文件完整代码示例,这样我们就可以看到问题所在。从中提取的单个片段是不够的。@MikaelLepistöAdded runkit Example在有多条记录的情况下返回一个数组,在有一条记录的情况下返回一个对象。可能您只需检查!array.isArraydata{data=[data];}只是想一想:@Vatsal有一次,它返回了一个包含单个对象的数组。如果我无法解决它,那将是我的解决办法。我不明白为什么它不一致。尝试创建一个包含初始化和所有内容的单文件完整代码示例,这样我们就可以看到问题所在。从中提取的单个片段是不够的。@MikaelLepistöAdded runkit示例