i can't a create a model in NodeJS - javascript

when i try to create a model in NodeJS
const mongoose = require ('mongoose');
const User = mongoose.model('User',{
name:{
type:string
},
lastname:{
type:string
},
age: {
type:Number
}
})
module.exports = User;
**errorIS
**
C:\Users\models\user.js:6
type:string
^
ReferenceError: string is not defined
at Object.<anonymous> (C:\Users\moham\Desktop\backend\models\user.js:6:14)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (C:\Users\moham\Desktop\backend\server.js:2:14)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
Node.js v18.12.1

The error message is saying that the variable "string" is not defined. In Mongoose, when creating a model, the data types for the fields are typically specified as strings, not by using the JavaScript keyword "string". To fix this, you can change the following line:
name:{ type:string }, lastname:{ type:string }
to:
name:{ type:String }, lastname:{ type:String }
This should resolve the "ReferenceError: string is not defined" error.

Related

'classifierType is not a constructor'

I'm using the limdu library found here (https://www.npmjs.com/package/limdu#binary-classification). I'm trying to combine the 'Multi-Label Classification' section with the 'Input Normalization' section to make the outputs all be lowercase, but I keep getting the following error:
/Users/nickbrinsmade/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43
this.classifier = new opts.classifierType();
^
TypeError: opts.classifierType is not a constructor
at new EnhancedClassifier (/Users/myusr/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43:20)
at Object.<anonymous> (/Users/myusr/Desktop/limdutest/index.js:9:20)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
My code Is seen below:
var limdu = require('limdu');
var MyWinnow = limdu.classifiers.Winnow.bind(0, {retrain_count: 10});
var textClassifier = new limdu.classifiers.multilabel.BinaryRelevance({
binaryClassifierType: MyWinnow
});
intentClassifier = new limdu.classifiers.EnhancedClassifier({
classifierType: textClassifier, // same as in previous example
normalizer: limdu.features.LowerCaseNormalizer
}); //this declaration throws the error.
intentClassifier.trainBatch([
{input: {I:1,want:1,an:1,apple:1}, output: "APPLE"},
{input: {I:1,want:1,a:1,banana:1}, output: "BANANA"},
{input: {I:1,want:1,chips:1}, output: "CHIPS"}
]);
console.log(intentClassifier.classify({I:1,want:1,an:1,apple:1,and:1,a:1,banana:1}));

Error trying to run a javascript program from the terminal

I'm trying to run a javascript file prompt.js from the terminal but I'm getting this error.
$ node prompt.js
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module 'prompt'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/pathwang/Documents/School/Spring2019/CSC404/CSC404_MyHomework/Patrick_Hwang_CSC404_Hw1/prompt.js:1:77)
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)
This is what is in my prompt.js file:
var prompt = require('prompt');
var schema = {
properties: {
name: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
email: {
},
exam1: { type: 'number'},
exam2: { type: 'number'},
exam3: { type: 'number'}
}
};
var student = {
name: '',
id: '',
exam1: 60,
exam2: 60,
exam3: 60,
at1: true,
at2: true,
at3: true,
at4: true,
at5: true,
adjust: true,
final: 100,
letter: ' '
};
//
// Get two properties from the user: username and email
//
prompt.get(schema, function (err, result) {
//
// Log the results.
//
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
console.log(' exam1:', result.exam1);
console.log(' exam2:', result.exam2);
console.log(' exam3:', result.exam3);
var totalExam;
totalExam = (1/3) * (result.exam1 +
result.exam2 +
result.exam3);
console.log(' Average exam =', totalExam )
});
//
// Start the prompt
//
prompt.start();
I'm in the right directory and I can run other files in the project folder by using $ node test.js. I also can open prompt.js file from the terminal in a different project folder where I copied the source code from. I don't know why this is happening? I would appreciate the help thanks.
The line var prompt = require('prompt'); causing the error. The error is saying it can't find said module. Or the module is not part of the project it doesn't exist. The solution is to find the module called prompt and add the directory into the project folder. This will solve the problem.

NFC Error in nodejs

I was about to make a program that reads NFC cards via NFC reader.
when I execute my program i get the following errros...
/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:83
throw e
^
Error: libnfc.so.5: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (module.js:598:18)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/home/dotmark/Desktop/nfc2/node_modules/nfc/index.js:1:95)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
My script for loading nfc libraries is as follows...
var nfc = require('nfc').nfc
, util = require('util')
;
console.log('nfc.version(): ' + util.inspect(nfc.version(), { depth: null }));
// { name: 'libfnc', version: '1.7.0' }
console.log('nfc.scan(): ' + util.inspect(nfc.scan(), { depth: null }));
Any help would be really great..

JavaScript Exporting a class definition [duplicate]

This question already has answers here:
Destructuring in Node.JS
(3 answers)
Closed 7 years ago.
Can I export class definition in JavaScript? For example,
in file "HelloWorld.js":
'use strict';
class HelloWorld {
constructor(msg = 'Hello World~') {
this.message = msg;
}
sayHi() {
console.log(this.message);
}
}
module.exports = HelloWorld;
Then in "index.js"
'use strict';
var HelloWorld = require('HelloWorld');
var myObj = new HelloWorld;
myObj.sayHi();
if I do "node index.js", then I got the error below:
constructor(msg = 'Hello World~') {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/data/users/soltiho/fbsource/fbcode/video_templates/test_env/index.js:3:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
my node is v5.5.0
Node doesn't support destructuring (yet), and using destructuring would be wrong anyways. There is nothing special about importing/exporting a class. Import it like any other module:
var myValue = require('myVModule');

How to perform jsonschemavalidation through http?

I have defined a restservice and trying to do a jsonschema validation with paperwork:
var _ = require('underscore');
var validate = require('isvalid-express');
var paperwork=require('paperwork');
module.exports = function (app) {
app.post('/myroute', paperwork({
username: /[a-z0-9]+/,
password: String,
age: Number,
interests: [String],
jobs: [{
company: String,
role: String
}]
}, function (req, res) {
// ...
}));
};
This is the request I post this in postman to myroute:
{
username: 'brucewayne',
password: 'iambatman',
age: 36,
interests: ['Climbing', 'CQC', 'Cosplay'],
jobs: [{
company: 'Wayne Inc.',
role: 'CEO'
}]
}
However it throws an error:
TypeError: undefined is not a function
at module.exports (c:\heroku\newher\node_codechallenge\node_modules\paperwork\paperwork.js:129:5)
at module.exports (c:\heroku\newher\node_codechallenge\app\testroutes.js:12:26)
at Object.<anonymous> (c:\heroku\newher\node_codechallenge\test.js:16:31)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain [as _onTimeout] (module.js:497:10)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
What is the best way of doing a json schemavalidation for http in node.js?
You may consider using JSON-schema standard rather than non-standard schema definition that paperwork uses.
There are many JavaScript JSON Schema validators, here is the benchmark: https://github.com/ebdrup/json-schema-benchmark

Categories