Add a Create Note API

Good call. Glad you figured it out.

You saved me man, I literally was wrecking my head about why such a simple command wouldn’t work and would simply go to a new line almost like it was executed but not outputting anything. (Some old issues even pointed to a serverless version not being up to date ). And when I read this and when I realized that somehow I was outside of the scope of my root folder I did a strong facepalm. Thanks a lot :slight_smile:

1 Like

Thanks for that, helped me out, as I was having problems due to eslint formatting rules :slight_smile: This solved it :slight_smile:

1 Like

I keep getting statusCode 500 with ‘Requested resource not found’. I didn’t notice the ‘env example’ file, I’ve renamed it to ‘.env’, added

tableName=notes

but that doesn’t seem to solve the problem as I keep getting ‘{ ResourceNotFoundException: Requested resource not found’

Here’s the content of my .env.

tableName=notes

This is what the serverless.yaml contains:

    environment:
        tableName: notes

I’ve also checked the table in the DB and it’s named ‘notes’

UPDATE:

for some reason I had my table and everything else created on the region:us-east-2 and all the settings in serverless.yml were using us-east-1 :slight_smile: changed it to 2 and it worked :wink:

also, it’d be good to mention in the tutorial that you NEED to create .env and add there tableName=nameOfYourTableInDynamoDB

I know that there’s a README, but when I followed the tutorial, I didn’t focus on exploring the files. perhaps my bad!

1 Like

Glad you figured it out!

A week ago, version 7.0 of the uuid package was released.

This release had a breaking change from the prior version of 3.x, the default export was removed.

The code in create.js errors out when run with: “TypeError: Cannot read property ‘v1’ of undefined” on line 22:

noteId: uuid.v1(),

To fix this, the import statement in line 1 should be changed to:

import { v1 as uuidv1 } from 'uuid';

And then line 22 should be:

noteId: uuidv1(),

*** If you were unlucky enough to install the 7.0.0 version, it introduced another bug when invoking the Lambda. “Error: uuid: This browser does not seem to support crypto.getRandomValues(). If you need to support this browser, please provide a custom random number generator through options.rng.” To resolve this, npm upgrade uuid to get 7.0.1 or higher. See issue 378: https://github.com/uuidjs/uuid/issues/378 ***

I hope this helps anyone else who gets stuck here.

I’m new to this guide, but I can also do a PR for the change if that makes sense

3 Likes

I just found this after going through all the pain and solution myself - I checked here to add a comment but see you already did!

Calling the serverless invoke local --function create --path mocks/create-event.json command throws the Serverless: DOTENV: Could not find .env file error. What could I be missing?

      Serverless: DOTENV: Could not find .env file.
Serverless: Bundling with Webpack...

ERROR in C:/temp/notes-app-api/create.js
Module build failed (from C:/temp/notes-app-api/node_modules/eslint-loader/index.js):
Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.resolve (internal/modules/cjs/helpers.js:32:19)
    at Plugins.load (C:\temp\notes-app-api\node_modules\eslint\lib\config\plugins.js:104:29)
    at Array.forEach (<anonymous>)
    at Plugins.loadAll (C:\temp\notes-app-api\node_modules\eslint\lib\config\plugins.js:165:21)
    at loadFromDisk (C:\temp\notes-app-api\node_modules\eslint\lib\config\config-file.js:529:35)
    at Object.load (C:\temp\notes-app-api\node_modules\eslint\lib\config\config-file.js:587:20)
    at Config.getPersonalConfig (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:167:37)
    at Config.getLocalConfigHierarchy (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:261:41)
    at Config.getConfigHierarchy (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:192:43)
    at Config.getConfigVector (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:299:21)
    at Config.getConfig (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:342:29)
    at processText (C:\temp\notes-app-api\node_modules\eslint\lib\cli-engine.js:181:33)
    at CLIEngine.executeOnText (C:\temp\notes-app-api\node_modules\eslint\lib\cli-engine.js:690:40)
    at lint (C:\temp\notes-app-api\node_modules\eslint-loader\index.js:278:17)
    at transform (C:\temp\notes-app-api\node_modules\eslint-loader\index.js:252:18)
    at C:\temp\notes-app-api\node_modules\loader-fs-cache\index.js:127:18
    at ReadFileContext.<anonymous> (C:\temp\notes-app-api\node_modules\loader-fs-cache\index.js:31:14)
    at ReadFileContext.callback (C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:115:16)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:238:13)
 
  Error --------------------------------------------------

  Error: Webpack compilation error, see above
      at _.forEach.compileStats (C:\temp\notes-app-api\node_modules\serverless-webpack\lib\compile.js:38:19)
      at arrayEach (C:\temp\notes-app-api\node_modules\lodash\lodash.js:516:11)
      at Function.forEach (C:\temp\notes-app-api\node_modules\lodash\lodash.js:9342:14)
      at BbPromise.fromCallback.then.stats (C:\temp\notes-app-api\node_modules\serverless-webpack\lib\compile.js:31:11)
      at tryCatcher (C:\temp\notes-app-api\node_modules\bluebird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:547:31)
      at Promise._settlePromise (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:604:18)
      at Promise._settlePromise0 (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:649:10)
      at Promise._settlePromises (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:729:18)
      at Promise._fulfill (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:673:18)
      at C:\temp\notes-app-api\node_modules\bluebird\js\release\nodeback.js:42:21
      at finalCallback (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:254:12)
      at runWithDependencies.err (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:277:6)
      at done (C:\temp\notes-app-api\node_modules\neo-async\async.js:2931:13)
      at runCompilers (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:181:48)
      at err (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:188:7)
      at compiler.run (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:270:7)
      at finalCallback (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:257:39)
      at hooks.done.callAsync.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:306:14)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
      at AsyncSeriesHook.lazyCompileHook (C:\temp\notes-app-api\node_modules\tapable\lib\Hook.js:154:20)
      at emitRecords.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:304:22)
      at Compiler.emitRecords (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:499:39)
      at emitAssets.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:298:10)
      at hooks.afterEmit.callAsync.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:485:14)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
      at AsyncSeriesHook.lazyCompileHook (C:\temp\notes-app-api\node_modules\tapable\lib\Hook.js:154:20)
      at asyncLib.forEachLimit.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:482:27)
      at C:\temp\notes-app-api\node_modules\neo-async\async.js:2818:7
      at done (C:\temp\notes-app-api\node_modules\neo-async\async.js:3522:9)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
      at outputFileSystem.writeFile.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:464:33)
      at C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:136:16
      at C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:57:14
      at FSReqWrap.oncomplete (fs.js:141:20)
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              10.15.0
     Framework Version:         1.65.0
     Plugin Version:            3.4.1
     SDK Version:               2.3.0
     Components Version:        2.22.3

I was able to fix the issue by coping the .env file from ~\node_modules\serverless-dotenv-plugin\examples\simple-express-app to the root folder, I am now getting a Failed to load plugin react: Cannot find module 'eslint-plugin-react' error.

ERROR in C:/temp/notes-app-api/create.js
Module build failed (from C:/temp/notes-app-api/node_modules/eslint-loader/index.js):
Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.resolve (internal/modules/cjs/helpers.js:32:19)
    at Plugins.load (C:\temp\notes-app-api\node_modules\eslint\lib\config\plugins.js:104:29)
    at Array.forEach (<anonymous>)
    at Plugins.loadAll (C:\temp\notes-app-api\node_modules\eslint\lib\config\plugins.js:165:21)
    at loadFromDisk (C:\temp\notes-app-api\node_modules\eslint\lib\config\config-file.js:529:35)
    at Object.load (C:\temp\notes-app-api\node_modules\eslint\lib\config\config-file.js:587:20)
    at Config.getPersonalConfig (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:167:37)
    at Config.getLocalConfigHierarchy (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:261:41)
    at Config.getConfigHierarchy (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:192:43)
    at Config.getConfigVector (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:299:21)
    at Config.getConfig (C:\temp\notes-app-api\node_modules\eslint\lib\config.js:342:29)
    at processText (C:\temp\notes-app-api\node_modules\eslint\lib\cli-engine.js:181:33)
    at CLIEngine.executeOnText (C:\temp\notes-app-api\node_modules\eslint\lib\cli-engine.js:690:40)
    at lint (C:\temp\notes-app-api\node_modules\eslint-loader\index.js:278:17)
    at transform (C:\temp\notes-app-api\node_modules\eslint-loader\index.js:252:18)
    at C:\temp\notes-app-api\node_modules\loader-fs-cache\index.js:127:18
    at ReadFileContext.<anonymous> (C:\temp\notes-app-api\node_modules\loader-fs-cache\index.js:31:14)
    at ReadFileContext.callback (C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:115:16)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:238:13)
 
  Error --------------------------------------------------

  Error: Webpack compilation error, see above
      at _.forEach.compileStats (C:\temp\notes-app-api\node_modules\serverless-webpack\lib\compile.js:38:19)
      at arrayEach (C:\temp\notes-app-api\node_modules\lodash\lodash.js:516:11)
      at Function.forEach (C:\temp\notes-app-api\node_modules\lodash\lodash.js:9342:14)
      at BbPromise.fromCallback.then.stats (C:\temp\notes-app-api\node_modules\serverless-webpack\lib\compile.js:31:11)
      at tryCatcher (C:\temp\notes-app-api\node_modules\bluebird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:547:31)
      at Promise._settlePromise (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:604:18)
      at Promise._settlePromise0 (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:649:10)
      at Promise._settlePromises (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:729:18)
      at Promise._fulfill (C:\temp\notes-app-api\node_modules\bluebird\js\release\promise.js:673:18)
      at C:\temp\notes-app-api\node_modules\bluebird\js\release\nodeback.js:42:21
      at finalCallback (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:254:12)
      at runWithDependencies.err (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:277:6)
      at done (C:\temp\notes-app-api\node_modules\neo-async\async.js:2931:13)
      at runCompilers (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:181:48)
      at err (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:188:7)
      at compiler.run (C:\temp\notes-app-api\node_modules\webpack\lib\MultiCompiler.js:270:7)
      at finalCallback (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:257:39)
      at hooks.done.callAsync.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:306:14)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
      at AsyncSeriesHook.lazyCompileHook (C:\temp\notes-app-api\node_modules\tapable\lib\Hook.js:154:20)
      at emitRecords.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:304:22)
      at Compiler.emitRecords (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:499:39)
      at emitAssets.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:298:10)
      at hooks.afterEmit.callAsync.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:485:14)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
      at AsyncSeriesHook.lazyCompileHook (C:\temp\notes-app-api\node_modules\tapable\lib\Hook.js:154:20)
      at asyncLib.forEachLimit.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:482:27)
      at C:\temp\notes-app-api\node_modules\neo-async\async.js:2818:7
      at done (C:\temp\notes-app-api\node_modules\neo-async\async.js:3522:9)
      at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\temp\notes-app-api\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
      at outputFileSystem.writeFile.err (C:\temp\notes-app-api\node_modules\webpack\lib\Compiler.js:464:33)
      at C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:136:16
      at C:\Users\AjitGoel\AppData\Roaming\npm\node_modules\serverless\node_modules\graceful-fs\graceful-fs.js:57:14
      at FSReqWrap.oncomplete (fs.js:141:20)
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              10.15.0
     Framework Version:         1.65.0
     Plugin Version:            3.4.1
     SDK Version:               2.3.0
     Components Version:        2.22.3

calling: serverless invoke local --function create --path mocks/create-event.json

Serverless Error ---------------------------------------

“service” property is missing in serverless.yml

Your Environment Information ---------------------------
Operating System: win32
Node Version: 13.6.0
Framework Version: 1.65.0
Plugin Version: 3.4.1
SDK Version: 2.3.0
Components Version: 2.22.3

Any help much appreciated.

@ajitgoel This reference explains it all: https://www.npmjs.com/package/serverless-dotenv-plugin

After installing look fore the env.example file in your solution and follow the instructions. Hope this helps.

Problem solved by removing comments

Glad you figured it out.

1 Like

@untilawesome @aidanjl

Thanks for pointing it out guys. For now we are asking people to use the older version till we update the guide and the repos.

$ npm install uuid@3.4.0 --save
1 Like

Thanks for posting this, also spent ages tearing my hair out on this.

I’ve raised #456 to update the documentation.

1 Like

I have a question: with these API functions we are getting our TableName from process.env.tableName, is there any reason we can’t get the table name from the event? Is it less secure or anything? I want to be able to use some of these CRUD functions with two tables. For example I want my delete.js to be able to delete from tables/notes and tables/lists using userId and noteId, but it seems undoable when I’m getting TableName from the yaml environment variable.

Well the event object is what the client is going to pass in. You can do it that way. Of course maybe not expose the exact table name. It might be better to create two separate endpoints for this.

1 Like

When a invoke this command:

    serverless invoke local --function create --path mocks/create-event.json

I got this:

Serverless: DOTENV: Loading environment variables from .env:
 
  Serverless Error ---------------------------------------
 
  "lnode" is not a valid sub command. Run "serverless invoke" to see a more helpful error message for this command.
 
  Get Support --------------------------------------------
     Docs:          ****
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              13.12.0
     Framework Version:         1.67.0
     Plugin Version:            3.6.4
     SDK Version:               2.3.0
     Components Version:        2.28.0

Well, I’ve created a empty .env file. No properties, yet.

Nevermind. My environment was in trouble.

1 Like