我的代码需要帮助。我想要一个上一个和一个下一个按钮,这些将调用一个函数 viewBlogItem(direction,cat,blogid);
在该函数中,我将读取 json 文件,按“类别”进行分类。
每个 blogItem 都有一个 articleid 和一个类别,如果单击下一个按钮,我想要下一个 blogItem.articleid 并显示那个(我使用 append 来表示那个)。如果方向 == "next",则查看它是否有类别中的下一个项目,如果没有,则隐藏 $('.next')。上一个按钮 $('.previous') 也是如此
blogItems.json
{
"blogItem":[
{
"id": 1,
"title": "animals blog 1",
"category":"animals",
"text":"text",
"articleid":1
},
{
"id": 2,
"title": "lifestyle blog 1",
"category":"lifestyle",
"text":"text",
"articleid": 1
},
{
"id": 3,
"title": "animals blog 2",
"category":"animals",
"text":"text",
"articleid": 2
},
{
"id": 5,
"title": "animals blog 4",
"category":"dieren",
"text":"text",
"articleid":4
},
{
"id": 4,
"title": "animals blog 5",
"category":"animals",
"text":"text",
"articleid":3
}
] …Run Code Online (Sandbox Code Playgroud) 我想知道如何将promise对象转换为字符串.我正在调用一个API,它返回一个XML对象,使用axios和transform进行模板化.我的应用程序使用Express作为应用程序框架.我在promise中的.then中有一个console.log,它在命令行中返回控制台中的数据.
但是,当我想在index.html中处理这些数据时,我在控制台中收到了一个promise对象.我怎样才能将promise对象的'cover'部分放到我的index.html中?
项目文件:
./routes/index.js
router.get('/metadata', function(req,res,next){
var info = metadata.getMetadataOnix(params);
console.log('info:'+info);
res.json({ test : info });
});
Run Code Online (Sandbox Code Playgroud)
index.html,其中包含一个获取对document.ready进行调用的ajax get
$.get("/metadata", function(data){
// call to metadata
});
Run Code Online (Sandbox Code Playgroud)
metadata-onix.js - 我称之为api的文件
const axios = require('axios');
const transform = require('camaro');
exports.getMetadataOnix = function (id) {
const template = {
uitgever: '/Product/PublishingDetail/Publisher/PublisherName/text()',
cover: "./Product/CollateralDetail/SupportingResource[ResourceContentType=01]/ResourceVersion/ResourceLink/text()"
}
return axios({
method: 'get',
url: 'https://someapi/getMetadataOnix/'+id,
transformResponse: [function (data) {
"use strict";
data = transform(data, template);
return data;
}],
}).catch(function(err){
// log errors
console.log('error:'+err);
}).then(function(resp){
var …Run Code Online (Sandbox Code Playgroud) 我写了一些返回承诺的函数,从google analytics api获取数据.我想我写的东西叫做回调地狱..
有人可以帮我优化这些代码(或提供提示/最佳实践),因此它更具可读性.
var express = require('express');
var router = express.Router();
var googleAuth = require('google-oauth-jwt');
var google = require('googleapis');
var app = express();
module.exports.getGoogleData = function (jwtClient,analytics,VIEW_ID){
return new Promise(function(resolve, reject){
return getOrdersToday(jwtClient,analytics,VIEW_ID).then(function (orders) {
return getOnlineUsersToday(jwtClient,analytics,VIEW_ID).then(function (users) {
return getSearchedToday(jwtClient, analytics, VIEW_ID).then(function (searched){
return getPageviewsTodayAndUsersToday(jwtClient, analytics, VIEW_ID).then(function (pageviews){
var returndata =[
{
"Orders":orders,
"Onlineusers":users,
"searched":searched,
"pageviews":pageviews[0].pageviews,
"usersToday":pageviews[0].users
}
]
resolve(returndata);
});
});
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
示例getfunction
function getOrdersToday(jwtClient,analytics,view_id){
return new Promise(function(resolve,reject){
analytics.data.ga.get({
'auth':jwtClient,
'ids': view_id,
'metrics': 'ga:totalEvents', …Run Code Online (Sandbox Code Playgroud) 我想创建一个这样的对象:
"store1":
{
"isbn":"3129321903",
"title":"here comes the title",
"author":"author of the book"
},
{
"isbn":"3333333333",
"title":"title of second book",
"author":"author of second book"
}
"store2":
{
"isbn":"3333311111",
"title":"title of book from store2",
"author":"author of book from store2"
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
var storeArray = [];
for(var i = 0; i < response.data.bookInfo.length; i++){
var keys = Object.keys(response.data.bookInfo[i]);
var storename = keys.filter(key => key != 'isbn' && key != 'metadata'); // get storename
if(storeArray[storename] == undefined){
storeArray[storename] = response.data.bookInfo[i];
}
else{
storeArray[storename].push(response.data.bookInfo[i]);
} …Run Code Online (Sandbox Code Playgroud)