0

Simply what im trying to do is export a function that make a mongodb query and import it into a react component so i can call it there and display the data.

this is the error i keep getting: ./node_modules/require_optional/node_modules/resolve-from/index.js Module not found: Can't resolve 'module' in '/Users/paul/Desktop/TempProject/Dietx/dietxweb/node_modules/require_optional/node_modules/resolve-from'

react component, Diet.js:

import React, {Component} from  'react';
import getItemList from '../server/api.js';
import ReactList from 'react-list';




class Diet extends Component {
  constructor(props){
    super(props)
    this.state ={
      Calories : 3000,
      Items: [],
    }
  }
  componentWillMount(){

  }

  render(){
    return(
      <div className="diet-container">
        <p>lol</p>
      </div>
    )
  }
}
export default Diet;

API, api.js:

const mongo = require('mongodb');



export const getItemList = ()=>{
  var url = "mongodb://localhost:27017/food"

  return(
    mongo.connect(url)
    .then((db)=>{
      return db.collection('foodInfo')
    })
    .then((res)=>{
      return res.find().toArray()
    })
  )
}

1 Answer 1

1

Change

export const getItemList = ()=>{

to

export default function getItemList() {

The syntax you are using is importing the default member from the module but your module does not define a default member.

Alternatively, you could use the syntax

import {getItemList} from ...

Sign up to request clarification or add additional context in comments.

6 Comments

its gives me new error unexpected token(5:15) pointing at the const keyword
Hmm looking at the syntax on developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… it looks like you may not be able to export a const variable as a default. I will update the answer with the correct syntax.
now im back to the original error. ./node_modules/require_optional/node_modules/resolve-from/index.js Module not found: Can't resolve 'module' in '/Users/paul/Desktop/TempProject/Dietx/dietxweb/node_modules/require_optional/node_modules/resolve-from'
Make sure you have run npm install
i have. i even installed 'module' alone. in a react project the node module folder is supposed to sit outisde of the src and public folders right, like at the root of the directory?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.