[英]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.