NodeJs - Sequelize - Unrecognized datatype for attribute - node.js

I'm trying to make a model for my dcauser table. I get stuck because it returns an error 'Unrecognized datatype for attribute'. Now i don't know if i messed something up in the database or is sequelize not working correctly with my DB or did i mess something up in the code below. I followed some guides but i get always stuck on the same problem i mentioned above.
var Sequelize = require('sequelize');
var sequelize = new Sequelize('postgres://postgres:xpwdx#localhost:xportx/xdbx');
const UserSchema = sequelize.define(
'dcauser',
{
userid: {
type: Sequelize.Integer,
allowNull: false,
}
}
);
module.exports = UserSchema;
i get the following error:
C:\Users\luka\source\repos\dcaBotManager\node_modules\sequelize\lib\model.js:1005
throw new Error(`Unrecognized datatype for attribute "${this.name}.${name}"`);
^
Error: Unrecognized datatype for attribute "dcauser.userid"
at C:\Users\luka\source\repos\dcaBotManager\node_modules\sequelize\lib\model.js:1005:15
at C:\Users\luka\source\repos\dcaBotManager\node_modules\lodash\lodash.js:13401:38
at C:\Users\luka\source\repos\dcaBotManager\node_modules\lodash\lodash.js:4905:15
at baseForOwn (C:\Users\luka\source\repos\dcaBotManager\node_modules\lodash\lodash.js:2990:24)
at Function.mapValues (C:\Users\luka\source\repos\dcaBotManager\node_modules\lodash\lodash.js:13400:7)
at Function.init (C:\Users\luka\source\repos\dcaBotManager\node_modules\sequelize\lib\model.js:1001:28)
at Sequelize.define (C:\Users\luka\source\repos\dcaBotManager\node_modules\sequelize\lib\sequelize.js:428:11)
at Object.<anonymous> (C:\Users\luka\source\repos\dcaBotManager\src\models\user.model.js:14:30)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\luka\source\repos\dcaBotManager\src\models\index.js:2:23)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\luka\source\repos\dcaBotManager\src\config\passport.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)

The integer keyword must be in uppercase
{
userid: {
type: Sequelize.INTEGER,
allowNull: false,
}
}

INTEGER must be in uppercase. See
https://sequelize.org/v5/manual/data-types.html
Though you should start using DataTypes.INTEGER instead.
https://sequelize.org/master/variable/index.html#static-variable-DataTypes

Related

JS- why is context not passed in to child function?

I am trying to declare a logger object, then have a child function write to the log that was declared in the parent using the context. However the context appears to be undefined inside the children, whether I use an arrow function or not.
Is it possible to pass the winston object to the child function implicitly ie without having to pass a winston object as a parameter?
Error log output:
this main context: {}
undefined
l1 err: TypeError: Cannot read properties of undefined (reading 'winston')
at l1 (MYPATH\cw_log_test_helper.ts:4:19)
at run (MYPATH\cw_log_contexts.ts:30:7)
at Object.<anonymous> (MYPATH\cw_log_contexts.ts:39:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Module.m._compile (MYPATH\node_modules\ts-node\src\index.ts:1371:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (MYPATH\node_modules\ts-node\src\index.ts:1374:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
{ l1: [Function: l1], r1: [AsyncFunction: r1] }
r1 err: TypeError: Cannot read properties of undefined (reading 'error')
at r1 (MYPATH\cw_log_test_helper.ts:12:31)
at run (MYPATH\cw_log_contexts.ts:31:7)
at Object.<anonymous> (MYPATH\cw_log_contexts.ts:39:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Module.m._compile (MYPATH\node_modules\ts-node\src\index.ts:1371:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (MYPATH\node_modules\ts-node\src\index.ts:1374:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
bye
main.ts
var winston=require('winston');
var WinstonCloudWatch=require('winston-cloudwatch');
import { l1, r1 } from './cw_log_test_helper';
const AWS = require('aws-sdk');
AWS.config.update({
region: 'us-east-1',
});
var run=()=>{
let sname=`mylambda`
var self=winston.add(new WinstonCloudWatch({
name:sname,
cloudWatchLogs: new AWS.CloudWatchLogs(),
logGroupName: 'winston-testing',
logStreamName: sname
}));
console.log(`this main context:`,this)
winston.error('first'); //<<<<<<<<<<<<<<<<<<WORKS
l1(`hello`) //<<<<<<<<<<<<<<<<FAILS
r1('hi') //<<<<<<<<<<<<<<<<FAILS
// flushes the logs and clears setInterval
var transport = self.transports.find((t) => t.name === sname)
transport.kthxbye(function() {
console.log('bye');
});
}
run()
cw_log_test_helper.ts:
export var l1=function(ltxt:string){
console.log(this)
try{
(this as any).winston.error(ltxt);
}catch(e){
console.log(`l1 err:`,e)
}
}
export var r1 = async (ltxt:string) => {
console.log(this)
try{
(this as any).winston.error(ltxt);
}catch(e){
console.log(`r1 err:`,e)
}
}

Extending String prototype. Cannot both specifiy accessors and a value or writable attribute

I am creating this custom method inside Object, Array, and String. When I run it on the Azure function app it works, no issue there, but when I start to test it using Mocha/Chai. It gives me below error:
TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>
Here is my code,
// declare global{
interface String {
contains(s: string): boolean;
}
interface Object {
contains(s: string) : boolean;
}
// }
// String
String.prototype.contains = function (s: string) : boolean {
return String(this).includes(s);
};
// Object
Object.prototype.contains = function (s: string): boolean {
return Object(this).hasOwnProperty(s);
}
//export {};
I import it into my function.
Note: This function works well in live/local on its own.
import 'custom/prototype/file';
export const myFunction : Function = (s: string) : Object => {
const bool : boolean = s.contains('look for this string');
const obj = { name: "string" };
const fbool : boolean = obj.contains('name');
return {
foo: s
fboo: fbool
};
}
Now in my test.spec.ts file I ran a test using the myFunction above.
I do not understand why it's giving me this error when it has no issues running by itself.
import { myFunction } from 'index/file';
import 'custom/prototype/file';
describe.skip('test case', () => {
it('test',() => {
const object = myFunction('this is a random string');
});
});
Here is the full error log:
TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>
at Function.defineProperty (<anonymous>)
at Object.<anonymous> (D:\test1\test\test azure\utils\defaultContext.ts:2:8)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Module.m._compile (D:\test1\test\test azure\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.require.extensions.<computed> [as .ts] (D:\test1\test\test azure\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (D:\test1\test\test azure\tests\DV_Musculoskeletal.spec.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Module.m._compile (D:\test1\test\test azure\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.require.extensions.<computed> [as .ts] (D:\test1\test\test azure\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.implementationOfRequireOrImportForUnstableEsm [as requireOrImport] (D:\test1\test\test azure\node_modules\mocha\lib\esm-utils.js:101:12)
at Object.exports.loadFilesAsync (D:\test1\test\test azure\node_modules\mocha\lib\esm-utils.js:88:34)
at Mocha.loadFilesAsync (D:\test1\test\test azure\node_modules\mocha\lib\mocha.js:467:19)
at singleRun (D:\test1\test\test azure\node_modules\mocha\lib\cli\run-helpers.js:125:15)
at exports.runMocha (D:\test1\test\test azure\node_modules\mocha\lib\cli\run-helpers.js:190:10)
at Object.exports.handler (D:\test1\test\test azure\node_modules\mocha\lib\cli\run.js:366:11)
at D:\test1\test\test azure\node_modules\yargs\build\index.cjs:443:71

cant make a validation to mongoose schema with nestjs-joi lib

i am trying to use joi validation with mongoose schema in nestjs but there is an error i couldn't solve it
and here is my code
schema.dto.ts
import Joi from "joi"
import { JoiSchema, JoiSchemaOptions } from "nestjs-joi"
#JoiSchemaOptions({
allowUnknown: false,
})
export class EmployeesJoi {
#JoiSchema(Joi.string().required())fullName!: string;
}
and here a small demo for my controller
#Post('AddNew')
async add(#Body() employee: EmployeesJoi ) {
await console.log(employee)
}
}
and finally, I faced that error
nest start
H:\000\training-center\dist\employees\dto\employees.dto.js:21
nestjs_joi_1.JoiSchema(joi_1.default.string().required()),
^
TypeError: Cannot read property 'string' of undefined
at Object.<anonymous> (H:\000\training-center\dist\employees\dto\employees.dto.js:21:42)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (H:\000\training-center\dist\employees\employees.controller.js:18:25)
I'm pretty sure joi's exports are named exports, so you need to do
import * as Joi from 'joi';
instead of
import Joi from 'joi';

TypeError: contracts_build_directory is not a function

i am trying to make a simple React/Truffle project.
Truffle-config file:
module.exports = {
contracts_build_directory: "./src/contracts",
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*",
}
},
mocha: {
},
compilers: {
solc: {
}
}
};
The migrations work and json files get created. However, when i run truffle test
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
TypeError: contracts_build_directory is not a function
at Object.<anonymous> (C:\Users\disst\Desktop\github\smart-contract\test\marketTest.js:7:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at C:\Users\disst\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:390:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:\Users\disst\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:387:14)
at Mocha.run (C:\Users\disst\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:961:10)
at resolve (C:\Users\disst\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\testing\Test.js:149:1)
at new Promise (<anonymous>)
at Object.run (C:\Users\disst\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\testing\Test.js:148:1)
at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.1.59 (core: 5.1.59)
Node v10.16.0
I've been searching stackoverflow for a while but couldn't find a solution.
You should use build_directory instead of contracts_build_directory

Pushing Array of Objects Mongoose NodeJS

I have a mongoose schema defined as such,
var userSchema = mongoose.Schema({
token: String,
school: [{name: String, grade: String}]
});
As you can see it is "school" with two values of strings inside it.
I am trying to push in values like this,
app.post('/array', function(req, res) {
user.find({
token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
}, function(err, users) {
if (users.length != 0) {
user.findOne({
token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
}, function(err, doc) {
doc.ar.push("sss", "A");
doc.save();
});
} else {}
});
});
But the push does not work and gives me an error that I cannot get rid of.
doc.ar.push({"sss","A"});
^
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> (C:\..\app.js:27:1)
at Module._compile (module.js:413:34)
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 Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
Is there a way to easily push values in?
That's a simple JavaScript syntax error. You probably want this:
doc.ar.push({name: "sss", grade: "A"});

Resources