throw new TypeError('2nd argument to `Model` must be a POJO or string, ' + [closed] - node.js

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
i just created my node js server and when i specified the schema and model and added it ins the main server file it is showing following error:
E:\py\.vscode\mern stack\backend\node_modules\mongoose\lib\model.js:115
throw new TypeError('2nd argument to `Model` must be a POJO or string, ' +
^
TypeError: 2nd argument to `Model` must be a POJO or string, **not** a schema. Make sure you're calling `mongoose.model()`, not `mongoose.Model()`.
at Mongoose.Model (E:\py\.vscode\mern stack\backend\node_modules\mongoose\lib\model.js:115:11)
at Object.<anonymous> (E:\py\.vscode\mern stack\backend\user.js:7:27)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (E:\py\.vscode\mern stack\backend\server.js:2:14)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
and the code of schema and model is as given below:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String
})
module.exports = mongoose.Model('USER', userSchema);

just write mongoose.model instead of mongoose.Model. I hope problem will be solved.

Related

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object

For
const systemRegex = /^system\./,
endOfLine = require('os').EOL,
EJSON = require('mongodb-extjson');
I got error at the line EJSON = require('mongodb-extjson'), details is as follows:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received function hidden
at Function.from (buffer.js:331:9)
at fnv1a32 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:21:25)
at fnv1a24 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:39:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/objectid.js:14:20)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/bson.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19) {
code: 'ERR_INVALID_ARG_TYPE'
}
How to resolve it?
It is because of the Node.js version.
Node.js v10.15.1: Works!
Node.js v12.8.1: Error loading the module
I am not sure about the cause, but I would guess that it is because Node.js' packaging mechanism has undergone a change. And mongodb-extjson has not been modified to adhere to the new mechanism. As mentioned in their GitHub repository, it is unlikely to be fixed as it is no longer being maintained.
You have the following choices:
Downgrade to v10 of Node.js, OR
Use the recommended bson module instead:
$ npm install bson
const { EJSON } = require('bson');
const data = '{ "someId": { "$oid": "5ec7cb151a1878fbefce4119" } }'
const doc = EJSON.parse(data, { relaxed: false });
console.log(doc.someId._bsontype)
// Should print 'ObjectID'
Note the difference in the EJSON option between the two modules. Whereas in mongodb-extjson the default is strict, in bson the default is relaxed.

Getting error while trying to save file in grid-fs using mongoose and nodejs

Trying to get GridFSBucket working for mongoose but no luck even after spending 2 days on it. I was getting a warning related to GridStore so thought of using GridFSBucket as we are using mongoose so referred the link but the same code is not working for me.
Following is the code I tried -
var express = require('express');
var gridfsService = express.Router();
var mongoose = require('mongoose');
const conn = mongoose.createConnection('mongodb://user:password#127.0.0.1:27017/dbname');
const gridFsBucket = new mongoose.mongo.GridFSBucket(conn.db);
The error I am getting is -
_chunksCollection: db.collection(options.bucketName + '.chunks'),
^
TypeError: Cannot read property 'collection' of undefined
at new GridFSBucket (d:\Projects\myapp\node_modules\mongoose\node_modules\mongodb\lib\gridfs-stream\index.js:50:27)
at Object.<anonymous> (d:\Projects\myapp\services\myapp.service.js:6:22)
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>
I am using following versions - Nodejs-8.11.2 Mongoose- 5.0.0 mongodb - 4.0.4
Had to downgrade mongoose version from latest seeing to suggestions. It worked on mongoose version 4.13.17 (legacy). Any other solution/suggestion is most welcome.

Node Error - Cannot read property of undefined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm attempting to write a node module that works with a node package. I'm kind of stuck, and don't know where the problem is. I know the package is being loaded correctly.
The Code Below is referencing the icontrol package
https://github.com/nfarina/icontrol/blob/master/index.js
// loads the icontrol package
var iControl = require("node-icontrol").iControl;
// these are the paramters you need
// according to this https://github.com/nfarina/icontrol/blob/master/index.js#L19-L22
var config = {
system: "system name",
email: "your#email.com",
password: "kdkdkdkdk",
pinCode: "pin"
}
// then you will want to initialize it
var mySystem = iControl(config);
// then there are a few calls you can make on mySystem
// 1. getArmState https://github.com/nfarina/icontrol/blob/master/index.js#L56
mySystem.getArmState(function(error, result){
if (error) {
console.error(error);
return
}
console.log(result);
});
// 2. setArmState https://github.com/nfarina/icontrol/blob/master/index.js#L70
// it looks like the armState param can be "disarm" or "arm"
mySystem.setArmState(armState, function(error){
if (error) {
console.error(error);
return
}
console.log("Alarm set to:", armState);
});
// 3. subscribeEvents https://github.com/nfarina/icontrol/blob/master/index.js#L96
// this will open a web socket listener that will send a message any
// time the arm state is changed
Error I'm receiving
mySystem.getArmState(function(error, result){
^
TypeError: Cannot read property 'getArmState' of undefined
at Object.<anonymous> (/Users/Admin/Documents/Test.js:15:9)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
can anyone help ?
You forgot the new operator. The iControl function is meant to be used as a constructor (with new). It doesn't have an explicit return statement, so without new it returns undefined.
Change:
var mySystem = iControl(config);
To:
var mySystem = new iControl(config);

How do I make generator functions work with Hapi JS

I installed various node.js versions with nvm and tried -harmony flag to make generator functions work with yield keyword but I'm getting all kinds of errors when the server starts. One of them is below:
/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:310
throw castErr;
^
TypeError: Cannot read property '_items' of undefined
at /home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:39:22
at Array.forEach (native)
at internals.Topo.add (/home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:36:24)
at internals.Object.keys (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:301:18)
at internals.root.root.object (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/index.js:71:72)
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/hapi/node_modules/catbox/lib/policy.js:255:24)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
Is there something special I need to do to use the yield keyword?
UPDATE: I updated nodejs to 5.4.1 and the errors are gone. But I can't use the yield function.
Here's a code:
var nodes = yield Db.node.find({ type: 'root' });
return reply.success(nodes);
And here's the error I get:
var nodes = yield Db.node.find({ type: 'root' });
^^
SyntaxError: Unexpected identifier
You'll need a plugin for hapi that allows you to run a generator as handler. Something like https://github.com/ide/hapi-async-handler

Mongoose Model alias error

I have a funny issue with Mongoose, one of MongoDB's ODMs.
I wanted to alias the mongoose.model method into simply Model. I even checked the alias :
exports = Model = mongoose.model;
console.log(Model === mongoose.model); // returns true
I already did this for mongoose.Schema and it worked seamlessly.
Now when I register a schema using the aliased Model variable :
Model('User', UserSchema);
I get the following error :
/node_modules/mongoose/lib/index.js:257
if (!this.modelSchemas[name]) {
^
TypeError: Cannot read property 'User' of undefined
at Mongoose.model (/node_modules/mongoose/lib/index.js:257:25)
at Object.<anonymous> (/app/models/user.js:20:1)
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.require (module.js:364:17)
at require (module.js:380:17)
at controllers_path (/app.js:23:2)
at Array.forEach (native)
But if I use the normal form, I get absolutely no errors :
mongoose.model('User', UserSchema);
Is this a bug in the Mongoose.js ODM or am I missing something ?
When you call mongoose.model(...), the mongoose object is getting passed into the model function as this. When you call the function through your alias, this will be set to global instead of mongoose.
If you really wanted to do this you'd have to do something like:
var Model = mongoose.model.bind(mongoose);
That way, mongoose gets passed into the function no matter how you call Model.
Just to elaborate on #JohnnyHK answer:
var a = {
b:function(){
console.log(this.name)
},
name:"its a"
}
a.b() //logs "its a"
var c = a.b;
c(); //logs undefined
While calling c the invoking context is window or the global object.

Resources