John John
John John

Reputation: 1475

Error Export Module in Node.js - separation of concerns

I am trying to implement separation of concerns by using export module. All the code is working if used without separation of concern but as soon as I am trying to import generateUrlArray() from const db = require('../db') nothing is working. Nodejs is not giving me any error on the back-end. The error I am getting on front-end is Error: SyntaxError: Unexpected end of JSON input . I am positive that the error is coming from back-end. Let me know if you have any ideas.

controller.js

const db = require('../db')

exports.getWebApiList = (req, res) => {
  (async function fetchDataList() {
    try {
      const urlArray = await db.generateUrlArray({}, { _id: 0 })
      return res.send(urlArray)
    } catch (ex) {
      console.log(`fetchDataList error: ${ex}`)
    }
  })()
}

..db/index.js

const { List } = require('./models/List')

const generateUrlArray = (query, projection) => {
  const dataFromDB = List.find(query, projection).select('symbol')
  return linkArray = dataFromDB.map(item => {
    return link = `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${item.symbol}&apikey=6BUYSS9QR8Y9HH15`
  })
}

module.exports = { generateUrlArray }

.models/List.js

const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const ParentSchemaSymbolList = new mongoose.Schema({
  symbol: String
})
module.exports.List = mongoose.model('List', ParentSchemaSymbolList)

Upvotes: 0

Views: 82

Answers (1)

AlexOwl
AlexOwl

Reputation: 867

const generateUrlArray = async (query, projection) => {
  const dataFromDB = await List.find(query, projection).select('symbol')
  const linkArray = dataFromDB.map(item => {
    return link = `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${item.symbol}&apikey=6BUYSS9QR8Y9HH15`
  })
  return linkArray
}

Upvotes: 1

Related Questions