Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js Express-如果数据不是';你不同意吗?_Javascript_Node.js_Express_Pug - Fatal编程技术网

Javascript Node.js Express-如果数据不是';你不同意吗?

Javascript Node.js Express-如果数据不是';你不同意吗?,javascript,node.js,express,pug,Javascript,Node.js,Express,Pug,我使用Simon Holmes的Mongo、Express、Angular和Node的Getting MEAN,在第7.2.3章使用API响应数据部分中,有一些非常奇怪的行为。似乎html是从res.render()生成的,而数据并没有实际传递给它。当传递正确的数据时,根据jade文件的操作方式,会发生不同的错误。以下是locations.js、index.js和locations list.jade文件,包括显示预期JSON的终端输出。所有文件都位于此处显示的app_server文件夹中 对于

我使用Simon Holmes的Mongo、Express、Angular和Node的Getting MEAN,在第7.2.3章使用API响应数据部分中,有一些非常奇怪的行为。似乎html是从res.render()生成的,而数据并没有实际传递给它。当传递正确的数据时,根据jade文件的操作方式,会发生不同的错误。以下是locations.jsindex.jslocations list.jade文件,包括显示预期JSON的终端输出。所有文件都位于此处显示的app_server文件夹中

对于书中没有提到的人,这个问题只涉及调用的
module.exports.homelist=function(req,res){…}
,当浏览器在localhost:3000/上发出请求时,就会发生这种情况。所有其他请求,如localhost:3000/location的行为符合预期。上一段中提到的关键数据是来自请求对象内部名为body的api请求的响应。res.render()函数将此数据用于位置,称为responseBody

有几个选项可以将代码注释掉或取消注释,从而产生不同的输出,如下所示。这发生在renderompacemodule.exports.homelist函数中的locations.js文件中,选项四除外。以下是选项

  • var renderHomepage=function(req,res,responseBody){…}
    中删除
    locations:responseBody
    将生成此图中显示的以下html
  • 包括
    locations:responseBody
    for
    var renderHomepage=function(req,res,responseBody){…}
    生成此图中显示的以下html
  • 使用
    var renderHomepage=function(req,res){…}
    生成与第一个选项相同的输出
  • 位置列表中删除第24-26行(所有涉及设施的内容-请参阅第二个选项中的错误消息)。jade并使用包含
    responseBody
    的选项二,会产生超过一百个空位置列表,因此可以向下滚动一段时间

  • 使用选项1、3和4从浏览器请求localhost:3000时的终端输出。选项二与之类似,只是在适当的位置有
    GET/500 806.003 ms-3396

    GET /api/locations?lng=-122.236353&lat=37.485217 200 19.818 ms - 297
    [{"distance":285.97478710956176,"name":"Peet's Coffee","rating":0,"facilities":["hot drinks"," food"," classical music"],"_id":"5ae25bd42d69c6abe46ae307"},{"distance":700.0951415645783,"name":"Philz Coffee","rating":4,"facilities":["hot drinks"," food"," power"],"_id":"5ae25c472d69c6abe46ae30a"}]
    GET / 304 128.077 ms - -
    GET /bootstrap/css/amelia.bootstrap.css 304 3.148 ms - -
    GET /stylesheets/style.css 304 4.557 ms - -
    GET /javascripts/jquery-1.12.4.min.js 304 8.150 ms - -
    GET /bootstrap/js/bootstrap.min.js 304 6.275 ms - -
    GET /bootstrap/fonts/glyphicons-halflings-regular.woff 304 0.551 ms - -
    
    locations.js

    var request = require('request');
    var apiOptions = {
      server : "http://localhost:3000"
    };
    if (process.env.NODE_ENV === 'production'){
      apiOptions.server = "https://example.herokuapp.com/"; // not actual location
    }
    
    // var renderHomepage = function(req, res) { // uncomment for Option Three
    //   res.render('locations-list', {
    //     title: 'Loc8r - find a place to work with wifi',
    //     pageHeader: {
    //       title: 'Loc8r',
    //       strapline: 'Find places to work with wifi near you!'
    //     },
    //     sidebar: "Looking for wifi and a seat? Loc8r helps you find places to work when out and about.  Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.",
    //   });
    // };
    
    var renderHomepage = function(req, res, responseBody) { // comment out for Option Three
      res.render('locations-list', {
        title: 'Loc8r - find a place to work with wifi',
        pageHeader: {
          title: 'Loc8r',
          strapline: 'Find places to work with wifi near you!'
        },
        sidebar: "Looking for wifi and a seat? Loc8r helps you find places to work when out and about.  Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.",
        // locations: responseBody // comment out for Option One, uncomment out for Option Two
      });
    };
    
    /* GET 'home' page */
    module.exports.homelist = function(req, res) {
      var requestOptions, path;
      path = '/api/locations';
      requestOptions = {
        url: apiOptions.server + path,
        method : "GET",
        JSON : {},
        qs : {
          lng : -122.236353,
          lat : 37.485217
        }
      };
      request(
        requestOptions,
        function(err, response, body){
          console.log(body);
          // renderHomepage(req, res);  // use for Option Three
          renderHomepage(req, res, body); // use for Option One and Two
        }
      );
    };
    
    /* GET 'Location info' page */
    module.exports.locationInfo = function(req, res) {
        res.render('location-info', {
        title: 'Location info',
        name: 'Starcups',
        address: '125 High Street, Reading, RG6 1PS',
        rating: 5,
        facilities: ['Hot drinks', 'Premium wifi'],
        distance: '109m',
        description: " Simon's cafe is on Loc8r because it has accessible wifi and space to sit down with your laptop and get some work dont.",
        times: [
          "Monday - Friday : 7:00am - 7:00pm",
          "Saturday : 8:00am - 5:00pm",
          "Sunday : closed"
        ],
        reviews: [{
          name: "Simon Holmes",
          description: "What a great place. I can't say enough good things about it.",
          timestamp: "16 Febuary 2014",
          rating: 3
        },{
          name: "Judy Holmes",
          description: "It was okay. Coffee wasn't great, but the wifi was fast.",
          timestamp: "26 January 2015",
          rating: 3
        },{
          name: "Simon Cramp",
          description: "It was okay. Coffee wasn't great, but the wifi was fast.",
          timestamp: "6 September 2012",
          rating: 3
        },{
          name: "Simoolmes",
          description: "What a great place. I can't say enough good things about it.",
          timestamp: "14 June 2013",
          rating: 3
        }]
      });
    };
    
    /* GET 'Add review' page */
    module.exports.addReview = function(req, res) {
        res.render('location-review-form', {
        title: 'Add review',
        name: 'Starcups' 
      });
    }; 
    
    地点-list.jade

    extends layout
    
    include _includes/sharedHTMLfunctions.jade
    
    block content
      #banner.page-header
        .row
          .col-lg-6
            h1= pageHeader.title
              small  #{pageHeader.strapline}
      .row
        .col-xs-12.col-sm-8
          .error= message
          .row.list-group
            each location in locations
              .col-xs-12.list-group-item
                h4
                  a(href="/location/#{location._id}")= location.name
                  small  
                    +outputRating(location.rating)
                  span.badge.pull-right.badge-default= location.distance
                p.address= location.address
                p
                  each facility in location.facilities
                    span.label.label-warning= facility
                    |                
        .col-xs.12.col-sm-4
          p.lead= sidebar
    
    index.js

    var express = require('express');
    var router = express.Router();
    var ctrlLocations = require('../controllers/locations');
    var ctrlOthers = require('../controllers/others');
    
    /* Location pages */
    router.get('/', ctrlLocations.homelist);
    router.get('/location', ctrlLocations.locationInfo);
    router.get('/location/review/new', ctrlLocations.addReview);
    
    /* Other pages */
    router.get('/about', ctrlOthers.about);
    
    module.exports = router;
    
    所以最大的问题是html如何显示没有传递到res.render()中的位置,如第一个和第三个选项所示?选项一和选项三显然非常相似,但我不得不再次检查,因为它看起来太奇怪了。我认为数据可能被缓存在某个地方,所以我更改了requestOptions中的经纬度数字,html显示了选项1和选项3的更改


    这里没有显示,我已经用本书前面的硬编码示例位置数据替换了responseBody,类似于在
    module.exports.locationInfo=function(req,res){…}
    中看到的内容,所有内容看起来都很好。

    抱歉,但我无法说出选项1、2和3是什么代码。我看到对
    res.render()
    的几个调用,这些调用将数据作为第三个参数传递。对
    res.render()
    的调用是在呈现的HTML中包含数据,但没有向呈现调用传递任何数据?请在问题中单独列出具体的代码,以便于查看。@jfriend00我已尝试通过在新的第二段中添加信息来澄清。在选项一和选项二的
    位置中:responseBody
    被注释掉,但显示在呈现的HTML中。