clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

Lambda Error: "module initialization error: ReferenceError" #112

Closed iDVB closed 6 years ago

iDVB commented 6 years ago

Can't seem to get dynogels working in lambda. With it, I'm always getting the CloudWatch error module initialization error: ReferenceError

Even just simply trying to define a model is enough to cause this issue...

import dynogels from "dynogels";
import joi from "joi";

dynogels.AWS.config.update({ region: "us-east-1" });

var BlogPost = dynogels.define("BlogPost", {
  hashKey: "email",
  rangeKey: "title",
  schema: {
    email: Joi.string().email(),
    title: Joi.string(),
    content: Joi.binary(),
    tags: dynogels.types.stringSet()
  }
});

I already have my IAM rules set but still not sure if this is a permission issue?

cdhowie commented 6 years ago

I'm closing this report for two reasons:

1) Lambda doesn't even support the import syntax... how are you invoking this at all?

Syntax error in module 'index': SyntaxError
(function (exports, require, module, __filename, __dirname) { import dynogels from "dynogels";
^^^^^^
SyntaxError: Unexpected token import

2) The likely source of the error (assuming you are transpiling) is that you have imported joi as joi but are using it as Joi (note the capitalization) which is likely the source of the ReferenceError.

iDVB commented 6 years ago

@cdhowie I'm pasting you the pre-babel-ified code. 😄 But I found part of my issue. Very stupid one. import joi from 'joi'; but then using it as Joi

However, I'm now getting the error that .get in not a function of undefined which is likely in reference to my Video.get model. The table is already in dynamo.... I'm simply trying to read from it? Am I doing something wrong?

cdhowie commented 6 years ago

@iDVB

You probably are doing something wrong, but it's hard to tell without any example code. For what it's worth, I tried this code:

const dynogels = require('dynogels');
const Joi = require('joi');

dynogels.AWS.config.update({ region: "us-east-1" });

var BlogPost = dynogels.define("BlogPost", {
  hashKey: "email",
  rangeKey: "title",
  schema: {
    email: Joi.string().email(),
    title: Joi.string(),
    content: Joi.binary(),
    tags: dynogels.types.stringSet()
  }
});

exports.handler = (event, context, callback) => {
    BlogPost.get('test', callback);
};

And it "works" (well, fails because I didn't create the BlogPost table, but it fails because the table isn't there, not because it can't find the BlogPost.get method).

iDVB commented 6 years ago

I found my issue. User Error of course. Was under the assumption that the methods return neat objects of the items queried. But I guess you have to grab the models from ... resp.Items.attrs. I also was expecting the methods to be async. (No idea why, your docs said callback everywhere. But quickly made use of dynogels-promisified.

All working like a charm now. Thanks!

Nice lib btw.