node.js并将我的sqlite数据传递给我的一个视图 [英] node.js and express passing sqlite data to one of my views

查看:134
本文介绍了node.js并将我的sqlite数据传递给我的一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的app.js中,我尝试从sqlite数据库中检索数据并将其传递给我的一个视图:

  app.get(/ dynamic,function(req,res){
var db = new sqlite3.Database(mainDatabase)
var posts = []

db.serialize(function(){
db.each(SELECT * FROM blog_posts,function(err,row){
posts.push({title:row.post_title,date:row。 post_date,text:row.post_text})
})
})

res.render(dynamic,{title:Dynamic,posts:posts})
})

有人可以告诉我我在这里做错什么。帖子数组似乎留空空的人物什么。



编辑
我正在追踪教程,解释说虽然插件具有异步,但这种方法不是异步的。



这里是教程


尽管Node.js的回调和异步性质,这些
事务将一系列运行,允许我们创建,插入,和
查询,知道先前的语句将在当前
之前运行。但是,sqlite3提供了一个具有相同
接口的并行包装器,但并行运行所有事务。

解决方案

db 调用可能是异步的。这意味着您在使用数据返回之前进行渲染。



您需要找出如何从查询中获取一个回调,并在该回调中呈现模板。 / p>

看起来你想要第二个完成回调传递给 db.each() 谢谢, Jonathan Lonowski ,提示!)

  var posts = []; 
db.serialize(function(){
db.each(SELECT * FROM blog_posts,function(err,row){
posts.push({title:row.post_title,date :row.post_date,text:row.post_text})
},function(){
//全部完成提取记录,渲染响应
res.render(dynamic,{title: 动态,帖子:帖子})
})
})

这个想法是在任何异步代码的最后回调中渲染,这样你就可以拥有所需的一切。





In my app.js I have the following to try to retrieve data from a sqlite database and pass it to one of my views:

app.get("/dynamic", function(req, res) {
    var db = new sqlite3.Database(mainDatabase)
    var posts = []

    db.serialize(function() {
        db.each("SELECT * FROM blog_posts", function(err, row) {
            posts.push({title: row.post_title, date: row.post_date, text: row.post_text})
        })
    })

    res.render("dynamic", {title: "Dynamic", posts: posts})
})

Can someone tell me what I am doing wrong here. The posts array seems to stay empty nomatter what.

EDIT I was following a tutorial that explained that though the plugin has async, this method is not asynchronous

Here is a quote from the tutorial

Despite the callbacks and asynchronous nature of Node.js, these transactions will run in series, allowing us to create, insert, and query knowing that the statement prior will run before the current one does. However, sqlite3 provides a "parallel" wrapper with the same interface, but runs all the transactions in parallel. It just all depends on your current circumstances.

解决方案

the db calls are likely asynchronous. Which means you are rendering before they return with their data.

You need to figure out how to get one callback from your query, and render your template in that callback.

It looks like you want a second complete callback passed to db.each() (Thanks, Jonathan Lonowski, for the tip!)

var posts = [];
db.serialize(function() {
    db.each("SELECT * FROM blog_posts", function(err, row) {
        posts.push({title: row.post_title, date: row.post_date, text: row.post_text})
    }, function() {
        // All done fetching records, render response
        res.render("dynamic", {title: "Dynamic", posts: posts})
    })
})

The idea is the render in the last callback of any asynchronous code, that way you have everything you need.


这篇关于node.js并将我的sqlite数据传递给我的一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆