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
via Himanshu Patel
No comments:
Post a Comment