javascript - 如何将数据从 module.export 函数传递到对象

标签 javascript node.js express

我有一个简单的 Node/Express 应用程序,并尝试将数据从 javascript 函数传递到模板(由 jade 提供支持)。

JavaScript 函数如下所示:

module.exports = {
  getFeatures: function() {
    var request = require("request")

    // ID of the Google Spreadsheet + Base URL
    var spreadsheetID = "abcdefg-123456";
    var sheetID = "od6";
    var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/" + sheetID + "/public/values?alt=json";

    //empty array for features
    var features = [];

    //get the features
    request({
        url: url,
        json: true
    }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            var data = body.feed.entry;
            data.forEach(function(item) {
                var obj = {
                    pub: item.gsx$publication.$t,
                    date: item.gsx$date.$t,
                    title: item.gsx$title.$t,
                    url: item.gsx$url.$t,
                }
                features.push(obj);
            });
            console.log("features", features"); //prints array containing all objects to server console
            return features;
        }
    });
  }
};

主应用程序如下所示:

'use strict';

var express = require('express');
var jade = require('jade');

var gsheets = require("./gsheets.js"); //pulls in module.exports from above
var featuresOld = require('../private/features.json'); //original json pull (a single array of objects)

var port = process.env.PORT || 3000;

var app = express();

// defining middleweare
app.use('/static', express.static(__dirname + '../../public'));

app.set('view engine', 'jade');
app.set('views', __dirname + '/templates');

...

// features route
app.get('/features', function(req, res) {
  var path = req.path;
  res.locals.path = path;
  var features = gsheets.getFeatures(); //attempting to call js function above
  res.render('features', {features: features}); //trying to pass data into a template
});

第一个函数成功将对象数组打印到服务器控制台,所以我认为错误在于我在主 app.js 中调用它的方式。 (请注意,只有当我将其输入为 gsheets.getFeatures(); 时才会打印,而不是 var features = gsheets.getFeatures();。)

另请注意,featuresOld 变量是一个已成功传递到 Jade 模板的对象数组,因此错误不在 res.render('features' , {features: features}); 行。

我确信这非常简单,但我似乎无法弄清楚。非常感谢任何帮助,谢谢。

最佳答案

我建议您研究 Promises( native 或使用像 Bluebird 这样的库)。 但是,如果不使用 Promise 或生成器并保持简单,您可以传递一个回调函数,该函数仅在检索值时调用。在此函数中,您可以渲染模板。

(请注意,您的函数当前不返回任何内容)

module.exports = {
  getFeatures: function(callback) {
    var request = require("request")

    // ID of the Google Spreadsheet + Base URL
    var spreadsheetID = "abcdefg-123456";
    var sheetID = "od6";
    var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/" + sheetID + "/public/values?alt=json";

    //empty array for features
    var features = [];

    //get the features
    request({
        url: url,
        json: true
    }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            var data = body.feed.entry;
            data.forEach(function(item) {
                var obj = {
                    pub: item.gsx$publication.$t,
                    date: item.gsx$date.$t,
                    title: item.gsx$title.$t,
                    url: item.gsx$url.$t,
                }
                features.push(obj);
            });
            console.log("features", features"); //prints array containing all objects to server console
            callback(features); // call the rendering function once the values are available
        }
    });
  }
};

现在在您的主应用程序中,您只需将回调传递给函数

app.get('/features', function(req, res) {
  var path = req.path;
  res.locals.path = path;
  gsheets.getFeatures(function(features) {
    res.render('features', {features: features}); //trying to pass data into a template
  });
});

基本上,您的请求函数是异步的 - 请求将在后台运行,并且在检索到该值后将使用该值调用回调函数。与此同时,其余代码将继续运行(在您的情况下,您将尝试使用该值,即使它尚未被检索)。 如果您需要执行一些取决于该值的操作,那么您必须将该代码放入回调函数中,该函数将在该值可用时被调用(如上所示)。 Promise 提供了一个很好的 API 来做到这一点。 ES6 还有一些新功能可以帮助您更好地组织异步代码。

关于javascript - 如何将数据从 module.export 函数传递到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34667006/

相关文章:

javascript - 带有 mongodb 点符号的变量

node.js - 无法使用 express-generator 工具引导 Express 项目

node.js - 有没有办法稍后使用 EJS 和 nodejs/express 添加 CSS/JS

javascript - 如何使用 ADAL JS 获取当前用户角色

node.js - 如何将 .aac url 流式传输到 wit.ai 语音 api

javascript - 告诉 typescript 不要将 ["default' ] 添加到新类

javascript - Twilio sendMessage 未发送到数组中的所有值

javascript - 访问链中后面的变量

javascript - WebGL/GLSL时间变量类似于ShaderToy

javascript - 改变元素的背景颜色