[英]nodejs why is my code returning [object Object] instead of an array?
所以我正在建立一个网站并使用 nodejs 进行一堆 api 调用并填充所有信息。 我有一个主页,它有一个侧边栏,人们可以在其中按“类别”进行排序。 但是当我运行我的代码而不是显示在 html 中的类别时,我得到[object Object]
。 我已经尝试了很多东西,但它仍然只返回[object Object]
。 这是代码:
索引.js
const express = require('express')
const router = new express.Router();
const categoriesFunction = require('../controllers/categories')
// get index page
router.get('', async (req, res) => {
let requestString = '/apps'
allApps = await openChannelRequest(requestString, req, res)
await res.render('index', {
categories: categoriesFunction,
// this is where I try to add the categories to the homepage
})
})
这是我抓取所有类别数据并存储它的控制器。 我很确定我可以在 index.js 页面中做到这一点,但几周前当我开始这样做时,我出于某种原因制作了控制器。 如果这不是最好的方法,请告诉我。
类别.js
const express = require('express')
const app = express()
var request = require('request');
var categoricalNames = []
var options = {
'method': 'GET',
'url': 'a working url',
'headers': {
'Authorization': 'working authorization'
},
'contentType': 'application/json'
};
var categoriesFunction = async() => request(options, function (error, response) {
return new Promise(function(resolve, reject){
console.log('inside categories function')
var categoryName = ''
if(error || !response)
{
// report error
reject(new Error('Try Again'));
}
else
{
//process response
var body = JSON.parse(response.body);
var jsonResponse = body.values
jsonResponse.forEach(function(obj) {
// console.log(obj.label)
categoryName = obj.label
// JSON.stringify(categoryName)
categoricalNames.push(categoryName)
});
categoricalNames.push({categoryName});
// console.log(categoricalNames)
// report success
JSON.stringify(categoricalNames)
resolve(categoricalNames);
}
})
});
module.exports.getPlaceDetails = categoriesFunction;
有一段时间我认为我的代码不起作用,但我的categoriesFunction
函数中的 console.logs 显示数组正在正确填充。 它只是没有被正确地推送到索引。 在 index.js 页面上尝试该方法对我没有任何作用。 仍然只是获取[object Object]
。 不知道为什么。 任何人都可以向我解释这一点吗?
谢谢!
我想出了我需要做什么。 基本上需要编写一个更干净的 Promise 并确保不返回数组而是解析数组。 并且还需要在 index.js 中有一个变量来awaits
categoriesFunction()
的结果
这就是 index.js 现在的样子
const express = require('express')
const router = new express.Router();
const categoriesFunction = require('../controllers/categories')
// get index page
router.get('', async (req, res) => {
let requestString = '/apps'
allApps = await openChannelRequest(requestString, req, res)
const thing = await categoriesFunction()
// this is important cause the method needs to be called before its inserted
await res.render('index', {
categories: thing,
// this is where I try to add the categories to the homepage
})
})
这是 category.js 文件的样子
let categoriesFunction = function() {
return new Promise(resolve =>
request(options,
(error, response) => {
var categoryName = ''
var body = JSON.parse(response.body);
var jsonResponse = body.values
jsonResponse.forEach(function(obj) {
categoryName = obj.label
JSON.stringify(categoryName)
categoricalNames.push(categoryName)
});
console.log(categoricalNames)
resolve(categoricalNames)
// now that this is resolved it can be returned
}
))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.