I have recently started Lambda function development on AWS. When try to import a JavaScript file I am getting the following error,
module initialization error: ReferenceError
at Object.<anonymous> (/var/task/model/claim_type.js:3:29)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/index.js:2:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
In root level I have the index.js file and claim_type.js file inside the model directory.
index.js
const Sequelize = require('sequelize');
const ClaimType = require('./model/claim_type.js');
exports.handler = function (event, context, callback) {
//function content
}
claim_type.js
const Sequelize = require('sequelize');
const ClaimType = sequelize.define('claimtype', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: Sequelize.STRING
}
}, {
timestamps: false
});
module.exports = ClaimType;
What is the correct way to import the claim_type.js?
const Sequelize = require('sequelize');
const ClaimType = sequelize.define('claimtype', {
your const is Camelcase, but the second line is lowercase!
Related
Im developing a nodejs application that needs to register to the AWS service discovery on the app start. I'm using the #aws-sdk/client-servicediscovery lib in my node application. As a reference, I am using the code from here: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-servicediscovery/index.html.
const { ServiceDiscoveryClient, CreateHttpNamespaceCommand } = require("#aws-sdk/client-servicediscovery");
const client = new ServiceDiscoveryClient({ region: "xxx" });
const params = {
"ServiceId":"xxx",
"InstanceId":"xxx",
"CreatorRequestId":new Date(new Date().toUTCString()),
"Attributes": {
AWS_INSTANCE_IPV4: "xxx.xx.xx.xx",
AWS_INSTANCE_PORT: xxx,
service: "xxx",
}
};
const command = new CreateHttpNamespaceCommand(params);
client.send(command).then(
(data) => {
console.log(data)
},
(error) => {
console.log(error)
}
);
While running the app, getting these errors.
/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5
const { readFile } = fs_1.promises;
^
TypeError: Cannot destructure property `readFile` of 'undefined' or 'null'.
at Object.<anonymous> (/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5:27)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/loadSharedConfigFiles.js:8:21)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
I am new to the AWS SD so need some help registering my service to the service discovery. Thanks in advance!
So I have a docker container (running a testing suite environment) that has stopped work on me. I'm using Mongoose 4.13.17. The problem I'm running into is every time I try to run my test cases it tries to connect to a testing db container but I get this error message.
Error: must pass in valid bson parser
at new Pool (/srv/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:105:13)
at Server.connect (/srv/app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:373:17)
at Server.connect (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:368:17)
at open (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/db.js:229:19)
at Db.open (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/db.js:252:44)
at createServer (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:391:6)
at /srv/app/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:512:14
at parseHandler (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:117:38)
at module.exports (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:97:5)
at connect (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:485:3)
at Function.MongoClient.connect (/srv/app/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:250:3)
at /srv/app/node_modules/mongoose/lib/connection.js:820:25
at Promise._execute (/srv/app/node_modules/bluebird/js/release/debuggability.js:313:9)
at Promise._resolveFromExecutor (/srv/app/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/srv/app/node_modules/bluebird/js/release/promise.js:79:10)
at NativeConnection.Connection.openUri (/srv/app/node_modules/mongoose/lib/connection.js:819:17)
at Mongoose.connect (/srv/app/node_modules/mongoose/lib/index.js:262:17)
at new App (/srv/app/src/app.ts:34:5)
at Object.<anonymous> (/srv/app/src/app.ts:80:16)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.replacementCompile (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at Module.m._compile (/srv/app/node_modules/ts-node/src/index.ts:439:23)
at module.exports (/srv/app/node_modules/nyc/node_modules/default-require-extensions/js.js:7:9)
at /srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4
at require.extensions.(anonymous function) (/srv/app/node_modules/ts-node/src/index.ts:442:12)
at Object.<anonymous> (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/srv/app/src/server.ts:6:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.replacementCompile (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at Module.m._compile (/srv/app/node_modules/ts-node/src/index.ts:439:23)
at module.exports (/srv/app/node_modules/nyc/node_modules/default-require-extensions/js.js:7:9)
at /srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4
at require.extensions.(anonymous function) (/srv/app/node_modules/ts-node/src/index.ts:442:12)
at Object.<anonymous> (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/srv/app/test/unit/rest/Audio.test.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.replacementCompile (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at Module.m._compile (/srv/app/node_modules/ts-node/src/index.ts:439:23)
at module.exports (/srv/app/node_modules/nyc/node_modules/default-require-extensions/js.js:7:9)
at /srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4
at require.extensions.(anonymous function) (/srv/app/node_modules/ts-node/src/index.ts:442:12)
at Object.<anonymous> (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at /srv/app/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/srv/app/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/srv/app/node_modules/mocha/lib/mocha.js:576:10)
at Object.<anonymous> (/srv/app/node_modules/mocha/bin/_mocha:637:18)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.replacementCompile (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.<anonymous> (/srv/app/node_modules/nyc/node_modules/append-transform/index.js:62:4)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at runMain (/root/.node-spawn-wrap-45-4803909aaae7/node:68:10)
at Function.<anonymous> (/root/.node-spawn-wrap-45-4803909aaae7/node:171:5)
at Object.<anonymous> (/srv/app/node_modules/nyc/bin/wrap.js:23:4)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at /root/.node-spawn-wrap-45-4803909aaae7/node:178:8
at Object.<anonymous> (/root/.node-spawn-wrap-45-4803909aaae7/node:181:3)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:279:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)
I've looked all around for a solution but it doesn't seem like this is an extremely common problem.
This is what my class looks like:
import * as bluebird from 'bluebird';
import { Application } from 'express';
import { connect, connection, disconnect, mongo } from 'mongoose';
import * as mongoose from 'mongoose';
import 'reflect-metadata'; // this shim is required for routing-controllers
import { createExpressServer, useContainer } from 'routing-controllers';
import { Container } from 'typedi';
import { getEnv } from './Service/Env';
// middleware
import { AuthenticateTokenMiddleware } from './Middleware/AuthenticateTokenMiddleware';
import { CorsHeadersMiddleware } from './Middleware/CorsHeadersMiddleware';
import { DisableAllExceptGetMiddleware } from './Middleware/DisableAllExceptGetMiddleware';
import { ParseTokenMiddleware } from './Middleware/ParseTokenMiddleware';
// controllers
import { init } from 'ooyala-express';
import { AudioController } from './Controller/V1/AudioController';
import { MovieController } from './Controller/V1/MovieController';
import { RootController } from './Controller/V1/RootController';
import { Indexer } from './indexer';
class App {
public express: Application;
constructor() {
(<any> mongoose).Promise = bluebird;
connect(getEnv('MONGODB_URI'), { useMongoClient: true });
useContainer(Container);
Indexer.ensureIndexes();
init({
ooyala_api_host: getEnv('ooyala_api_host'),
ooyala_cdn_host: getEnv('ooyala_cdn_host'),
ooyala_rights_locker_host: getEnv('ooyala_rights_locker_host'),
ooyala_player_host: getEnv('ooyala_player_host'),
player_expiration_window: getEnv('player_expiration_window'),
});
this.express = createExpressServer({
// Disable so we don't get a nasty serialized mongoose document.
// If they fix the issue with Expose/Exclude not being respected then
// perhaps this could be set back to true
classTransformer: false,
controllers: [
RootController,
AudioController,
MovieController,
],
middlewares: [
// Order is important. Middlewares are executed in index order.
CorsHeadersMiddleware,
DisableAllExceptGetMiddleware,
ParseTokenMiddleware,
AuthenticateTokenMiddleware,
],
});
}
}
export default new App().express;
New to node and mongodb
I am working on a linux Centos 7 enviorment.
For some reason I get this error 'TypeError: db.model is not a function'
What does this error means?
/var/www/html/mongo/crud/src/model/task.js:11
return db.model('tasks',Task);
^
TypeError: db.model is not a function
at module.exports (/var/www/html/mongo/crud/src/model/task.js:11:15)
at Object.<anonymous> (/var/www/html/mongo/crud/src/routes/index.js:4:39)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/html/mongo/crud/src/app.js:9:21)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
[nodemon] app crashed - waiting for file changes before starting...
this is the content of task.js file:
module.exports = function(){
var db = require('../libs/db-connection')();
var Schema = require('mongoose').Schema;
var Task = Schema({
title: String,
description: String,
status: Boolean
});
return db.model('tasks',Task);
}
This the content of index.js file:
const express = require('express');
const router = express.Router();
const model = require('../model/task')();
router.get('/' ,(req, res) => {
model.find({}, (err, tasks) =>{
if (err) throw err;
res.render('index', {
title: 'CRUD',
task: tasks
});
});
});
module.exports = router;
Unless you have a model function in your libs/db-connection, I think that you want to use the model from mongoose.
If this is right, to fix your problem you need to change the import from mongoose, like this:
var mongoose = require('mongoose'); // Not require('mongoose').Schema (because you need schema and model)
var Task = mongoose.Schema({ // Call Schema from mongoose
title: String,
description: String,
status: Boolean
});
return mongoose.model('tasks',Task); // Call model from mongoose
My code is as shown below:
app.js
const app = express();
const itemsRouter = require('./routes/items.js');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/items', itemsRouter);
items.js
const router = require('express').router;
router.get('/itemTest', (req, res) => {
res.json({
sucess: true
});
console.log(`the parametrs are ${req.body.item_name} ${req.body.item_post}`);
});
module.exports = router;
But here somehow, I am not able to get the router working and it says that can not read property 'get' of undefined.
the error stack is as shown below:
TypeError: Cannot read property 'get' of undefined
at Object.<anonymous> (C:\Users\anand\quFlipApi\routes\items.js:5:7)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\anand\quFlipApi\app.js:33:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
There are two problems here:
It's express.Router and not express.router.
You need to create a new instance of express.Router (e.g. var router = new express.Router()) instead of trying to using the constructor directly as an instance.
Here is my schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var test = new Schema({
name : {
type : String,
require : true,
sparse : true
},
description : {
type : String
},
questions : {
type : [Schema.Types.ObjectId],
sparse : true
} });
module.exports = new mongoose.model('test', test);
When I try to execute, it give me error as follows :
TypeError: Cannot read property 'test' of undefined
at new Mongoose.model (/home/utkarsh/Desktop/MEAN_REST_user_management/node_modules/mongoose/lib/index.js:329:25)
at Object.<anonymous> (/home/utkarsh/Desktop/MEAN_REST_user_management/app/models/test.js:19:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/utkarsh/Desktop/MEAN_REST_user_management/app/routes/test.js:6:13)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
I have used the same format earlier, it worked fine. Dont know why its not working. Can anyone help?
Change this:
module.exports = new mongoose.model('test', test);
To this:
module.exports = mongoose.model('test', test);
mongoose.model() isn't a class, so you shouldn't instantiate it (using new).