weixin_33747129 2017-05-04 17:14 采纳率: 0%
浏览 9

NodeJ和HTML

Below are the file and code i have but i am having issue when i put name in field you seen picture above i am not getting same input back (for Example: i put Abby than i get Same out put). I trying to put name from csv file than try to populate data. file when collecting data from csv file. i will past part of csv file at bottom. I need to first Populate the data which belong to each name by the year. Any help will be appreciated. Thanks. ( I am using mongoose and mongodb with Nodejs.

Index.js:

extends layout
block head
script(src="https://code.jquery.com/jquery-3.2.1.min.js",
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=",
crossorigin="anonymous")

block content
h1= title

input#input(type="text", placeholder = "Name", name="text")
ul#results
script(src="javascripts/NameSurf.js")

NameSurf.js

   $(document).ready(() => {

// set a listener on the textbox
$('#input').on("change", (evt) => {
    let text = $('#input').val();
    // the {text: text} sends a parameter named text with the
    $.get('/namesurfer', { text: text })
        .done((data) => {
            console.log(data);
             $('#results').prepend('<li>' + data['result'] + '</li>');
            $('#input').val('');   // reset the textbox
        })
        .fail((xhr) => {
            alert('Problem contacting server');
            console.log(xhr);
        });
  });
 });

**App.js**

const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();

// db setup
mongoose.connect('mongodb://localhost/namesurfers');

// define the schema
let nameSchema = new mongoose.Schema({
    name: String,
    "1900": Number,
    "1910": Number,
    "1920": Number,
    "1930": Number,
    "1940": Number,
    "1950": Number,
    "1960": Number,
    "1970": Number,
    "1980": Number,
    "1990": Number,
    "2000": Number,
    //  for number {type: Number, min: 0, max: 100}
});

// create the model
let NameSurfer = mongoose.model('namesurfer', nameSchema);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// the main page
app.get('/', (req, res) => {

    // res.send( '<h1> Hello, from Node!! </h1>');
    res.render('index', {title: 'NameSurfer'});
});


// Name surfer end point
app.get("/namesurfer", (req, res) => {
    let text = req.query.text;
    NameSurfer.find ({name: text}, (err, all_records) => {
        if (err) {
            console.log(err);
            res.json( {result: '***DB ERROR***'} );
        }
        else if (all_records == []) {
            res.json( {result: '***DB ERROR***'} );
        }
        else {
            all_records.forEach((rec) => {
                text = text.replace(new RegExp( rec.name ), rec['1910']);
            });
            res.json({result: text });
        }
    });
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  let err = new Error('Not Found');
  err.status = 404;
  next(err);
});



// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});



module.exports = app;

Csv file Data

name,1900,1910,1920,1930,1940,1950,1960,1970,1980,1990,2000 A,83,140,228,286,426,612,486,577,836,0,0 Aaliyah,0,0,0,0,0,0,0,0,0,380,215 Aaron,193,208,218,274,279,232,132,36,32,31,41 Abagail,0,0,0,0,0,0,0,0,0,0,958 Abbey,0,0,0,0,0,0,0,0,537,451,428 Abbie,431,552,742,924,0,0,0,0,752,644,601 Abbigail,0,0,0,0,0,0,0,0,0,953,562 Abby,0,0,0,0,0,906,782,548,233,211,209 Abdiel,0,0,0,0,0,0,0,0,0,925,721

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)