Knex/Jade:无法读取未定义的属性'length'



尝试切换Mongo并放入Postgres(换句话说,类似的代码在Mongo上工作得很好)。获取以下代码Cannot read property 'length' of undefined。我确定这很简单,但我是整个堆栈的新手,所以很难将它们捆绑在一起。knex上的例子不多,所以即使文档非常好,我也很难找到一个可以复制的例子。

我显然正在提取正确的数据,但卡在如何让玉来显示它。

堆栈:节点/快递/Knex/Postgres/Jade

用户.js

router.get('/', function(req, res, next) {
  db.select().from('users').limit(1)
    .then(function(users) {
        console.dir(users)})
    .then(function(users) {
        res.render('users', {users: users})})
    .catch(function(error) {
        console.error(error)
  })
});

用户.翡翠

extends layout
block content
  h1 Users
  ul
    for user in users
      p 
        input(type="text", name="name", value="#{user.name}")
        input(type="text", name="admin", value="#{user.admin}")
        input(type="text", name="smsNumber", value="#{user.smsNumber}")

安慰

[ { uid: 1,
    name: 'Don',
    admin: 'true',
    smsNumber: '4077023951',
    created_at: null,
    updated_at: null } ]

本地主机浏览器:3000/用户

C:Usersdvande03Personaldailychallengeviewsusers.jade:6 4| h1 Users 5| ul > 6| for user in users 7| p 8| input(type="text", name="name", value="#{user.name}") 9| input(type="text", name="admin", value="#{user.admin}") Cannot read property 'length' of undefined
TypeError: C:Usersdvande03Personaldailychallengeviewsusers.jade:6
    4|   h1 Users
    5|   ul
  > 6|     for user in users
    7|       p 
    8|         input(type="text", name="name", value="#{user.name}")
    9|         input(type="text", name="admin", value="#{user.admin}")
Cannot read property 'length' of undefined
    at eval (eval at <anonymous> (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:218:8), <anonymous>:51:31)
    at eval (eval at <anonymous> (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:218:8), <anonymous>:106:4)
    at eval (eval at <anonymous> (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:218:8), <anonymous>:119:22)
    at res (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:219:38)
    at Object.exports.renderFile (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:380:38)
    at Object.exports.renderFile (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:370:21)
    at View.exports.__express [as engine] (C:Usersdvande03Personaldailychallengenode_modulesjadelibindex.js:417:11)
    at View.render (C:Usersdvande03Personaldailychallengenode_modulesexpresslibview.js:126:8)
    at tryRender (C:Usersdvande03Personaldailychallengenode_modulesexpresslibapplication.js:639:10)
    at EventEmitter.render (C:Usersdvande03Personaldailychallengenode_modulesexpresslibapplication.js:591:3)
    at ServerResponse.render (C:Usersdvande03Personaldailychallengenode_modulesexpresslibresponse.js:961:7)
    at C:Usersdvande03Personaldailychallengeroutesusers.js:11:13
    at tryCatcher (C:Usersdvande03Personaldailychallengenode_modulesbluebirdjsmainutil.js:24:31)
    at Promise._settlePromiseFromHandler (C:Usersdvande03Personaldailychallengenode_modulesbluebirdjsmainpromise.js:454:31)
    at Promise._settlePromiseAt (C:Usersdvande03Personaldailychallengenode_modulesbluebirdjsmainpromise.js:530:18)
    at Promise._settlePromises (C:Usersdvande03Personaldailychallengenode_modulesbluebirdjsmainpromise.js:646:14)

res.render应如下所示: res.render(view [, locals] [, callback]) ,其中 locals 是一个对象,其属性定义视图的局部变量。在您的情况下,这意味着将渲染的行更改为:

res.render('users', {users: users})

传递一个包含要呈现的局部变量的对象。在 jade 文件中,当您想要引用值时,您可以使用对象中的键。

解决了

,但我不完全明白为什么。我在渲染之前注释掉了 console.dir,它可以工作。这是因为 console.dir .then中的用户没有传递到渲染.then吗?

我也接受了上面的答案,因为没有这个,我就不可能到达这里。谢谢@tomtom。

相关内容

  • 没有找到相关文章

最新更新