0

从另一个文件调用函数对我的 JS 文件不起作用。我无法从另一个 js 文件调用函数。我认为问题出在连接两个文件之间。

我的第一个 JS 文件

    const { response } = require('express');
var express = require('express');
const {render}=require('../app');
const productHelpers = require('../helpers/product-helpers');
var router = express.Router();
var productHelper=require('../helpers/product-helpers')





/* GET users listing. */
router.get('/', function(req, res, next) {

  productHelpers.getAllProduct().then((products)=>{
    
    res.render('admin/veiw-products', { admin: true, products })
  })

});
router.get('/add-product', function (req, res) {
  res.render('admin/add-product')

})

router.post('/add-product', (req, res) => {
  console.log(req.body)
  console.log(req.files.image)
  productHelpers.addProduct(req.body, (id) => {
    console.log(id)
    let image = req.files.image
    image.mv('./public/product-images/' + id + '.jpg', (err, done) => {
      if (!err) {
        res.render('admin/add-product')

      }else{
        console.log(err);
      }
    })


  })
})
router.get('/delete-product/:id',(req,res)=>{
  let proId=req.params.id
  console.log(proId);
  productHelpers.deleteProduct(proId).then((response)=>{
    res.redirect('/admin/')
  })
  
})


module.exports = router;

我的第二个 JS 文件

    var db = require('../config/connection')
var collection = require('../config/collections');
const {Delete} = require('../routes/admin');
module.exports = {



    addProduct: (product, callback) => {
        console.log(product);
        db.get().collection('product').insertOne(product).then((data) => {

            callback(data.ops[0]._id)

        })
    },

    getAllProduct:()=>{
        return new Promise(async(resolve,reject)=>{
            let products=await db.get().collection(collection.PRODUCT_COLLECTION).find().toArray()
            
            
            
            resolve(products)

        }) 
    },
    deleteProduct:(prodId)=>{
        return new Promise((resolve,reject)=>{
            db.get().collection(collection.PRODUCT_COLLECTION).removeOne({_id:prodId}).then((response)=>{
               console.log(response)
                resolve(response)
            })
        })
    }
    
}

我的错误报告

    productHelpers.getAllProduct is not a function
TypeError: productHelpers.getAllProduct is not a function
    at C:\Users\sainupangad\Desktop\newproject\routes\admin.js:15:18
    at Layer.handle [as handle_request] (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\sainupangad\Desktop\newproject\node_modules\express\lib\router\index.js:47:12)

另一份报告

 [nodemon] starting `node ./bin/www index.js`
express-session deprecated undefined resave option; provide resave option app.js:27:9express-session deprecated undefined saveUninitialized option; provide saveUninitialized option app.js:27:9
(node:7424) Warning: Accessing non-existent property 'render' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
Database connected
(node:7424) Warning: Accessing non-existent property 'getAllProduct' of module exports inside circular dependency
GET /admin 500 62.505 ms - 4079
4

1 回答 1

1

尝试使用

const { getAllProduct } = require('../helpers/product-helpers');
于 2021-04-27T22:03:44.700 回答