Node.js Handlebar:从异步处理程序在视图上呈现数据
我是新的车把特别定制处理 我正在尝试使用处理程序呈现Azure搜索中的数据并在视图中显示 Azure中的数据需要一些时间才能呈现,并且在视图中显示“[对象承诺]” 有人能帮我理解如何在主页上显示异步处理程序中的数据吗 提前谢谢Node.js Handlebar:从异步处理程序在视图上呈现数据,node.js,azure-cognitive-search,express-handlebars,Node.js,Azure Cognitive Search,Express Handlebars,我是新的车把特别定制处理 我正在尝试使用处理程序呈现Azure搜索中的数据并在视图中显示 Azure中的数据需要一些时间才能呈现,并且在视图中显示“[对象承诺]” 有人能帮我理解如何在主页上显示异步处理程序中的数据吗 提前谢谢 home.把手code: <div class="page-center "> <div class="row mb-3"> <div class="c
home.把手
code:
<div class="page-center ">
<div class="row mb-3">
<div class="col-3">
</div>
<div class="col-6">
<div class="input-group">
<div class="search"><input autocomplete="off" id="searchString" type="text" id="search-box"
class="form-control rounded-0" placeholder="What are you looking for?" value="">
<div></div>
</div>
<div class="input-group-btn "><button class="btn button-primary" type="submit">Search</button>
</div>
</div>
</div>
</div>
{{!-- {{#if (documents.length > 0)}} --}}
<div class="row mb-3">
{{#each customSearch}}
<div class="col-3">
{{!-- <img src="{{this.avatar}}"> --}}
<p>{{agency}} </p>
</div>
{{/each}}
</div>
{{!-- {{/if}} --}}
{{!-- <div class="pagination pagination-centered">
<ul>
{{#paginate pagination type="first"}}
<li {{#if disabled}}class="disabled" {{/if}}><a href="?p={{n}}">First</a></li>
{{/paginate}}
{{#paginate pagination type="previous"}}
<li {{#if disabled}}class="disabled" {{/if}}><a href="?p={{n}}">Prev</a></li>
{{/paginate}}
{{#paginate pagination type="middle" limit="7"}}
<li {{#if active}}class="active" {{/if}}><a href="?p={{n}}">{{n}}</a></li>
{{/paginate}}
{{#paginate pagination type="next"}}
<li {{#if disabled}}class="disabled" {{/if}}><a href="?p={{n}}">Next</a></li>
{{/paginate}}
{{#paginate pagination type="last"}}
<li {{#if disabled}}class="disabled" {{/if}}><a href="?p={{n}}">Last</a></li>
{{/paginate}}
</ul>
</div> --}}
</div>
home.js (router code)
const routeHome = (req, res, next) => {
const context = {
// customSearch: '',
helpers: {
json: function (value, options) {
return JSON.stringify(value);
},
customSearch: async () => {
// let data = main(name);
// return 'testing';
var self = this;
let documents = [];
let count = 0
let searchResults = await client.search("*", { top: 5 });
console.log('searchResults', searchResults.results)
// console.log('length', searchResults.results)
console.log(`Index operations succeeded: ${JSON.stringify(searchResults.results)}`);
// return JSON.stringify(searchResults.results);
for await (const result of searchResults.results) {
console.log('await call');
documents.push(result.document);
// console.log(`${JSON.stringify(result.document)}`);
}
context.documents = documents;
console.log('documents saved', documents);
return documents;
},
}
}
res.render('home', context)
}
app.js
var express = require('express');
var exphbs = require('express-handlebars');
var paginate = require('handlebars-paginate');
var Handlebars = require('handlebars');
const helpers = require('./helpers')
const path = require('path')
var app = express();
const hbs = exphbs.create({
partialsDir: __dirname + '/views/partials',
helpers: helpers()
})
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.enable('view cache');
app.set('views', __dirname + '/views');
Handlebars.registerHelper('paginate', paginate);
app.use(express.static(path.resolve(__dirname, 'public'))) // serve static files
const routeHome = require('./routes/home')
const routeAbout = require('./routes/about')
app.get('/', (req, res, next) => routeHome(req, res, next))
app.get('/about', (req, res, next) => routeAbout(req, res, next))
app.listen(3000, () => {
console.log('The web server has started on port 3000');
});
搜寻
{{!-{{#if(documents.length>0)}--}}
{{#每个自定义搜索}
{{!-- --}}
{{机构}
{{/每个}}
{{!---{/if}--}}
{{!--
{{{#paginate pagination type=“first”}
{{/paginate}
{{{#paginate pagination type=“previous”}
{{/paginate}
{{{#paginate pagination type=“middle”limit=“7”}
{{/paginate}
{{{#paginate pagination type=“next”}
{{/paginate}
{{{#paginate pagination type=“last”}
{{/paginate}
--}}
home.js(路由器代码)
const routeHome=(请求、恢复、下一步)=>{
常量上下文={
//自定义搜索:“”,
助手:{
json:函数(值、选项){
返回JSON.stringify(值);
},
customSearch:async()=>{
//让数据=主(名称);
//返回“测试”;
var self=这个;
让文档=[];
让计数=0
让searchResults=wait client.search(“*”,{top:5});
console.log('searchResults',searchResults.results)
//console.log('length',searchResults.results)
log(`Index操作成功:${JSON.stringify(searchResults.results)}`);
//返回JSON.stringify(searchResults.results);
用于等待(searchResults.results的常量结果){
log('wait call');
文档.推送(结果.文档);
//log(`${JSON.stringify(result.document)}`);
}
context.documents=文档;
console.log(“保存的文档”,文档);
归还文件;
},
}
}
res.render(“主”,上下文)
}
app.js
var express=需要(“express”);
var exphbs=require('express-handlebar');
var paginate=需要(“把手-paginate”);
var车把=需要(‘车把’);
const helpers=require(“./helpers”)
const path=require('路径')
var-app=express();
const hbs=exphbs.create({
partialsDir:uu dirname+'/views/partials',,
助手:助手()
})
附件发动机(“把手”,hbs发动机);
应用程序集(“查看引擎”、“把手”);
app.enable('view cache');
app.set('views','u dirname+'/views');
车把。登记帮助器('paginate',paginate);
app.use(express.static(path.resolve(\uu dirname,'public'))//服务静态文件
const routeHome=require(“./routes/home”)
const routeAbout=require(“./routes/about”)
app.get(“/”,(请求,res,next)=>routeHome(请求,res,next))
app.get('/about',(req,res,next)=>routeAbout(req,res,next))
app.listen(3000,()=>{
log(“web服务器已在端口3000上启动”);
});