gpt4 book ai didi

node.js - 回调错误 : TypeError: Cannot read property 'rows' of undefined

转载 作者:行者123 更新时间:2023-11-29 14:30:28 26 4
gpt4 key购买 nike

我要在我的产品列表中添加分页。但我一直有错误:

console.log(data.rows);
^

这是在我的数据库中查询的 js 文件:

list: (client, filter, callback) => {
const productListQuery = `
SELECT *
FROM products
ORDER BY id
`;
client.query(productListQuery, (req, result)=>{
console.log(result.rows);
callback(result.rows);
});

这是我的 router.get 在我的 server.js 中:

app.get('/products', function (req, res, next) {
Product.list(client, {limit: 8}, {offset: (req.query.p - 1) * 8}, {
}, function (products) {
res.render('/client/product-list', {
products: products,
title: 'Products',
pagination: {
page: req.query.p || 1,
limit: 8,
n: req.query.p || 1
}
});
});
});

最佳答案

这仅仅意味着您的数据undefined,而在JavaScript中undefined对象没有任何属性或值。

建议你去 JavaScript Documentation在使用它之前。

当您调用您的数据库时,响应出现错误,并且由于您没有正确处理响应而导致此错误。

要了解 undefined 和其他对象值之间的区别:

let a;
let b = 22;
let c = null;
let d = {
'a': 22,
'b': [4,5,6]
};

console.log(a); // undefined
console.log(a.b); // TypeError: Cannot read property 'b' of undefined
console.log(b); // 22
console.log(b.c); // undefined
console.log(c); // null
console.log(d.a); // 22
console.log(d.b); // [4,5,6]
console.log(e); // ReferenceError: e is not defined

关于node.js - 回调错误 : TypeError: Cannot read property 'rows' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52595805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com