1

我正在使用 MERN(MongoDB、Express、React、Node.js)构建一个全栈应用程序。

我有一个上传到 MongoDB 的 .CSV 文件。但是我无法让我的路线返回任何数据。我可以在我的 MongoDB 集群上看到数据库集合,但它没有出现在我的mongoshell 中。

我的思考过程就是这就是为什么我无法让我的路线工作。我该如何解决?

示例 .csv 文件数据

Date,Client,Project,Project Code,Hours,Billable?,First Name,Last Name,Billable Rate
,,,,,,,,
4/3/17,Anil,Ethereum,RD001,3.84,No,Robert,Powell,0
4/3/17,Olith,Pharos,DV002,7.69,Yes,Hubert,Green,80
4/3/17,Olith,Pharos,DV002,4.46,Yes,Bradley,Hale,80
4/3/17,Olith,Pharos,DV002,7.57,Yes,Rudy,Parker,80
4/3/17,Prosaria,Business Development,GM001,0.92,No,Walter,Silva,0

如您所见,数据存在于我的 mongo-atlas

在此处输入图像描述

但是当我打电话时我的 mongo-shell 是空的db.clients.find()

在此处输入图像描述

服务器.js

const express = require('express');
const app = express();
const mongodb = require('mongodb').MongoClient;
const fs = require('fs');
const fastcsv = require('fast-csv');
const csvtojson = require('csvtojson');
require("dotenv").config()

const PORT = process.env.PORT || 3003;


//middleware
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(express.static(__dirname + 'public'));


// Global Configuration
const MONGODBURI = process.env.MONGODBURI || 'mongodb://localhost:27017/projectData';


let stream = fs.createReadStream('./gm_data.csv');
let csvData = [];
let csvStream = fastcsv
  .parse()
  .on('data', (data) => {
    csvData.push({
      date: data[0],
      client: data[1],
      project: data[2],
      projectCode: data[3],
      hours: data[4],
      billable: data[5],
      firstName: data[6],
      lastName: data[7],
      billableRate: data[8]
    })
  })
  .on('end', () => {
    csvData.shift();
    console.log(csvData);

    mongodb.connect(
      MONGODBURI, {
        useNewUrlParser: true, 
        useUnifiedTopology: true
    },
      (err, client) => {
        if (err) throw err;
          client
            .db('gm-clients')
            .collection('clients')
            .insertMany(csvData, (err, res) => {
              if (err) throw err;
              console.log(`Inserted: ${res.insertedCount} rows`);
              client.close
            });
      }
    );
  });

stream.pipe(csvStream);


//TEST ROUTE --- // http://localhost:3003/
app.get('/', (req, res) => {
  res.send('Say Hello to Zee Server')
})


//routes
const customerRoutes = require('./routes/routes.js');
app.use('/customer', customerRoutes);


//Listening
app.listen(PORT, () => {
    console.log('Listening on port', PORT)
})

路由.js

const express = require('express')
const router = express.Router();
const path = require('path');
let fs = require('fs');
let parse = require('csv-parse');
const csv = require('csvtojson');

const Customer = require('../models/customer.js');


//Index Route -- //http://localhost:3003/customer/allcustomers
router.get('/allcustomers', (req, res) => {
    Customer.find({}, 'client', (err, data) => {
      res.json({data: data});
    })
  })


module.exports = router;

模型.js

const mongoose = require('mongoose')

const customerSchema = new mongoose.Schema({
    name: String, //project col in CSV
    clients: String, //client col in csv
    hours: Number, // hours col in csv
    billable_hours: Number, // billable col in csv
    billable_amount: Number // billable rate in csv
})

// name | clients | hours | billable hours | billable amount

module.exports = mongoose.model('Customer', customerSchema);

4

0 回答 0