如何将多个对象传递到单个手柄页面/模板中

时间:2017-08-23 15:56:38

标签: mysql node.js handlebars.js

我有两个mysql查询,每个查询返回为名为“ElementId”的列中的每个值“aboutUsDiv1”和“aboutUsDiv2”输入的最后一条记录。

我有一个把手模板,我希望在我的查询中显示两个对象(在不同的div中)。

如何将两个对象传递到我的手柄模板中并将它们显示在不同的div中?这可能吗?

ContentUpdate.js

0dp

AboutUs.handlebars

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:background="#545454">

    <android.support.v7.widget.AppCompatImageButton
        android:layout_width="55dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:id="@+id/imageButton"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />

    <TextView
        android:text="xxxxxx xxxxxxxxx"
        android:layout_width="140dp"
        android:layout_height="23dp"
        android:layout_marginStart="18dp"
        android:id="@+id/textView19"
        android:textSize="17dp"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_marginTop="9dp"
        android:layout_toEndOf="@+id/imageButton" />

    <TextView
        android:text="xxxxxxx(x) xxxxx"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:id="@+id/textView21"
        android:layout_below="@+id/textView19"
        android:textSize="12dp"
        android:layout_alignStart="@+id/textView19"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/messegesListView"
    android:divider="@null"
    android:dividerHeight="3dp"
    android:paddingBottom="6dp">
</ListView>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="43dp"
    android:background="#ffffff">
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="43dp"
    android:background="#ffffff">

    <EditText
        android:layout_width="325dp"
        android:layout_height="43dp"
        android:layout_alignParentBottom="true"
        android:hint="xxxxxx xxxxxxxxx..."
        android:background="@android:color/transparent"
        android:layout_alignParentStart="true"
        android:id="@+id/editText" />

    <Button
        android:text=">"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:id="@+id/button10"
        android:scaleType="fitCenter"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text=""
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_alignParentBottom="true"
        android:id="@+id/button11"
        android:layout_toStartOf="@+id/button10" />
</RelativeLayout>

app.js

exports.showAboutUs1 = function(req,res)
{
 req.getConnection(function(err, connection){
  connection.query('SELECT * FROM `editablecontent` WHERE ElementId = "AboutUsDiv1" ORDER BY `id` DESC LIMIT 1  ',[], function(err, result){
   return res.render('AboutUs', {
    data1: result,
    admin: req.session.admin,
    user: req.session.user
 });
console.log(data);
   });
  });
};

exports.showAboutUs2 = function(req,res)
{
  req.getConnection(function(err, connection) {
  connection.query('SELECT * FROM `editablecontent` WHERE ElementId = "AboutUsDiv2" ORDER BY `id` DESC LIMIT 1  ',[], function(err, result){
   return res.render('AboutUs', {
    data2: result,
    admin: req.session.admin,
    user: req.session.user
  });
 console.log(result);
    });
   });
 };

2 个答案:

答案 0 :(得分:1)

您的代码应该是这样的。

 exports.showAboutUs1 = function (req, res) {
  req.getConnection(function (err, connection) {
    connection.query('SELECT * FROM `editablecontent` WHERE ElementId = "AboutUsDiv1" ORDER BY `id` DESC LIMIT 1  ', [], function (err, result1) {
        connection.query('SELECT * FROM `editablecontent` WHERE ElementId = "AboutUsDiv2" ORDER BY `id` DESC LIMIT 1  ', [], function (err, result2) {
            return res.render('AboutUs', {
                data1: result1,
                data2: result2,
                admin: req.session.admin,
                user: req.session.user
            });
            console.log(result1);
            console.log(result2);
        });
    });
 });
};

<强> app.js

var contentUpdate = require("./functions/ContentUpdate");
...
app.get('/aboutUs', function(req,res){
contentUpdate.showAboutUs1;
});

答案 1 :(得分:0)

在将Mongo查询的2个对象发送到同一车把视图时,我遇到了同样的麻烦,按照上面评论的逻辑,我可以解决问题

我的有效代码:

router.get('/', (req, res) => {

Categorie.find(  function(err, categorie){
Post.find( function(err, post){
res.render('Painel_principal',{
  savedCategorie: categorie,
  savedPosts: post 
   })
  })
})

hbs视图:

    <a class="button" href="/admin/categories">Categorias</a>


    {{!-- Block for a new post --}}
    <div class="card">
      <div class="card-body">
          <form action="/admin/posts/new" method="post">
            <div class="form-group">
              <textarea class="form-control" id="content" name="content"placeholder="O que há de novo?" rows="3" required></textarea> 
            </div>
            <select class="custom-select" name="categorie" id="categorie" >
              <option selected>Selecione uma categoria</option>
              {{#if savedCategorie}}
                  {{#each savedCategorie}}
                  <option value="{{_id}}">{{nome}}</option>
                  {{/each}}
              {{else}}
                  <option value="0">Sem categorias</option>
              {{/if}}

            </select>

            <button type="submit" class="btn btn-primary mt-1">Postar</button>
          </form>
      </div>
    </div>

    {{!-- Listing posts --}}

    <div class="card mt-5 post">
      {{#if savedPosts}}
          {{#each savedPosts}}
                <div class="card-body">
                  <img src="img/igor.jpg" class="postProfilerPic" width="32px">
                  <small class="">Igor b.s</small>
                  <small class="float-right categorie">#categoreiName</small>
                  <p>
                    {{content}}
                  </p>
                  <small class="float-right postDate">Publicado em:{{date}}</small>
                </div>
          {{/each}}
      {{else}}
          <div class="card-body">
            Nenhum post
          </div>
      {{/if}}

    </div>