尝试切换出Mongo并放入Postgres(换句话说,类似的代码与Mongo一起工作正常)。获取Cannot read property 'length' of undefined
以下代码。我确信这很简单,但我是整个堆栈的新手,因此无法将它们捆绑在一起。关于knex的例子不多,所以即使文档非常好,我也很难找到要复制的例子。
我显然正在提取正确的数据,但我仍坚持如何让玉石显示它。
Stack:Node / Express / Knex / Postgres / Jade
users.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)
})
});
users.jade
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 } ]
localhost:3000 / users
的浏览器C:\Users\dvande03\Personal\dailychallenge\views\users.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:\Users\dvande03\Personal\dailychallenge\views\users.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:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:218:8), <anonymous>:51:31)
at eval (eval at <anonymous> (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:218:8), <anonymous>:106:4)
at eval (eval at <anonymous> (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:218:8), <anonymous>:119:22)
at res (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:219:38)
at Object.exports.renderFile (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:380:38)
at Object.exports.renderFile (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:370:21)
at View.exports.__express [as engine] (C:\Users\dvande03\Personal\dailychallenge\node_modules\jade\lib\index.js:417:11)
at View.render (C:\Users\dvande03\Personal\dailychallenge\node_modules\express\lib\view.js:126:8)
at tryRender (C:\Users\dvande03\Personal\dailychallenge\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (C:\Users\dvande03\Personal\dailychallenge\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\Users\dvande03\Personal\dailychallenge\node_modules\express\lib\response.js:961:7)
at C:\Users\dvande03\Personal\dailychallenge\routes\users.js:11:13
at tryCatcher (C:\Users\dvande03\Personal\dailychallenge\node_modules\bluebird\js\main\util.js:24:31)
at Promise._settlePromiseFromHandler (C:\Users\dvande03\Personal\dailychallenge\node_modules\bluebird\js\main\promise.js:454:31)
at Promise._settlePromiseAt (C:\Users\dvande03\Personal\dailychallenge\node_modules\bluebird\js\main\promise.js:530:18)
at Promise._settlePromises (C:\Users\dvande03\Personal\dailychallenge\node_modules\bluebird\js\main\promise.js:646:14)
答案 0 :(得分:0)
res.render应如下所示:let spawnAction = SKAction.repeatActionForever(
SKAction.sequence([
SKAction.waitForDuration(2),
SKAction.runBlock({
[unowned self] in
self.spawnEnemy()
})
])
)
runAction(spawnAction)
,其中res.render(view [, locals] [, callback])
是一个对象,其属性定义视图的局部变量。
在您的情况下,这意味着将您渲染的行更改为:
locals
传递一个带有局部变量的对象进行渲染。在jade文件中,当您想要引用值时,可以使用对象中的键。
答案 1 :(得分:0)
解决了它,但我不完全理解为什么。我在渲染之前注释掉了console.dir并且它可以工作。这是因为console.dir .then
中的用户没有传递给渲染.then
吗?
我也接受了上面的答案,因为没有它,我就无法到达这里。谢谢@tomtom。