Netlify Serverless functions Lambda Error

I was using netlifys amazing serverless functions in development (localhost) and I was testing certain things with MongoDB as well.

During this process I file that I didn’t add and I deleted it…
It looked like it was generated by netlify and it has maps of the js serverless functions I made.
This was the error

{'errorMessage': 'lambdaFunc[lambdaHandler] is not a function',
 'errorType': 'TypeError',
 'level': 'error',
 'stackTrace': ['Object._executeSync '
                '(C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lambdalocal.js:286:47)',
                'C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lambdalocal.js:95:26',
                'new Promise (<anonymous>)',
                'Object.execute '
                '(C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lambdalocal.js:87:16)',
                'Object.invokeFunction '
                '(C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\src\\lib\\functions\\runtimes\\js\\index.js:57:36)',
                'NetlifyFunction.invoke '
                '(C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\src\\lib\\functions\\netlify-function.js:82:41)',
                'processTicksAndRejections '
                '(internal/process/task_queues.js:95:5)',
                'handler '
                '(C:\\Users\\musaa\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\src\\lib\\functions\\server.js:110:33)']}

Code:

import mongoose from 'mongoose';
const { CPU, GPU, RAM } = require('../models/component_models.js')

exports.handler = async (event, context) => {
    console.log(process.env.URI)
    try {
        const Client = mongoose.connect(process.env.URI)
    } catch (error) {
        console.log(error + "testing")
        return {
            statusCode: 400,
            body: JSON.stringify({ message: "Error, couldnt connect." })
        }
    }

    console.log(event.body)
    const data = CPU(JSON.parse(event.body))
    data.save()

    return {
        statusCode: 200,
        body: event.body
    }
};

The file had generated itself again after I reran the netlify dev command however the error still remains.
image

Hey @Musa,

Could you share a repo?

Sure here is the repo Ill quickly update it rn GitHub - MusaMoses/computer_site.

Code should be updated with every single thing from my local directory
I belive it would help you debug better

Interesting, is this not working only in the CLI or is it same on production too?

I haven’t really put it in production (host it anywhere)
i was only developing it and that occured.

I actually deleted a netlify folder that popped up outta nowehere then I, noticed I made a mistake, ran the stuff and it threw me an error.

Any other ideas? This project is due soon so I need to fix it quickly!

You have a mix of CommonJS and ESM for your imports/exports.

When those are fixed, it runs correctly.

After a little testing (commenting out lines) it appears the issue stems from

const { CPU } = require('../db_config/component_models.js')

When this line is commented out (and the const data = CPU(JSON.parse(event.body)) associated with it) the function works.
If I call the function directly (http://localhost:8888/.netlify/functions/components) I see undefined logged to the console. If I load the homepage http://localhost:8888 I see the following logged to the console.

{"title":"RTX 3090","description":"This graphics card hits harder than ever!\nIt is the latest and greatest!","specifications":{"cudaCoreCount":10164,"clockSpeed":"10Mhgz"},"price":1700}
Response with status 200 in 2 ms.
Request from ::1: POST /.netlify/functions/components
undefined
{"title":"RTX 3090","description":"This graphics card hits harder than ever!\nIt is the latest and greatest!","specifications":{"cudaCoreCount":10164,"clockSpeed":"10Mhgz"},"price":1700}

Oh wow, it does throw an error because of the import. Genuinely don’t know why, do you guys have a clue? everything looks to me…

As I mentioned, you’ve mashed together CommonJS syntax and ESM syntax.

This for example:

const { CPU } = require('../db_config/component_models.js')

should be:

import { CPU } from '../db_config/component_models.js';

In the ./db_config/component_models.js file you have an ESM import at the top of the file:

import mongoose, { Schema } from 'mongoose';

But then a CommonJS export at the bottom:

module.exports = { CPU, GPU, RAM }

which should be done with export (export - JavaScript | MDN) instead.

For example:

export const GPU = mongoose.model( 'Graphics Card', component )
export const CPU = mongoose.model( 'Processor', component )
export const RAM = mongoose.model( 'RAM', component )
2 Likes

Thanks Alot! Not only fixed the problem but educated me some more!

Great to hear @Musa.

Excellent explanation @nathanmartin (apologies, missed your previous post while I was posting.)