I keep getting the above error when trying to run my app, can't figure out what package I haven't added..
The server was working perfectly fine before adding this document, so im hoping its an issue with this document and not something else.
Its a npm called node-scheduler
var express = require('express');
var router = express.Router()
var path = require('path');
var bodyParser = require('body-parser');
var db = require('mongoskin').db("localhost/awl-loan-system", { w: 0});
db.bind('event');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.get('/init', function(req, res){
db.event.insert({
text:"My test event A",
start_date: new Date(2013,8,1),
end_date: new Date(2013,8,5)
});
db.event.insert({
text:"My test event B",
start_date: new Date(2013,8,19),
end_date: new Date(2013,8,24)
});
db.event.insert({
text:"Morning event",
start_date: new Date(2013,8,4,4,0),
end_date: new Date(2013,8,4,14,0)
});
db.event.insert({
text:"One more test event",
start_date: new Date(2013,8,3),
end_date: new Date(2013,8,8),
color: "#DD8616"
});
res.send("Test events were added to the database")
});
app.get('/data', function(req, res){
db.event.find().toArray(function(err, data){
//set id property for all records
for (var i = 0; i < data.length; i++)
data[i].id = data[i]._id;
//output response
res.send(data);
});
});
app.post('/data', function(req, res){
var data = req.body;
var mode = data["!nativeeditor_status"];
var sid = data.id;
var tid = sid;
delete data.id;
delete data.gr_id;
delete data["!nativeeditor_status"];
function update_response(err, result){
if (err)
mode = "error";
else if (mode == "inserted")
tid = data._id;
res.setHeader("Content-Type","text/xml");
res.send("<data><action type='"+mode+"' sid='"+sid+"' tid='"+tid+"'/>
</data>");
}
if (mode == "updated")
db.event.updateById( sid, data, update_response);
else if (mode == "inserted")
db.event.insert(data, update_response);
else if (mode == `enter code here`"deleted")
db.event.removeById( sid, update_response);
else
res.send("Not supported operation");
});
returning this error
D:\One31\Clients\A W Lymn\node_modules\express\lib\express.js:107
throw new Error('Most middleware (like ' + name + ') is no longer bundled
with Express and must be installed separately. Please see
https://github.com/senchalabs/connect#middleware.');
^
Error: Most middleware (like bodyParser) is no longer bundled with Express
and must be installed separately. Please see
https://github.com/senchalabs/connect#middleware.
at Function.get (D:\One31\Clients\A W
Lymn\node_modules\express\lib\express.js:107:13)
at Object.<anonymous> (D:\One31\Clients\A W Lymn\routes\calendar.js:12:16)
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> (D:\One31\Clients\A W Lymn\routes\index.js:6:16)
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)
Any help?
You're already including the separate body-parser module, so use bodyParser.urlencoded({ extended: false })/bodyParser.json() instead of express.bodyParser().
You need to install body-parser separately by running
npm i body-parser -S
The -S flag adds the package to your package.json file
Related
> module.js:549
> throw err;
> ^
>
> Error: Cannot find module '..models/category'
> at Function.Module._resolveFilename (module.js:547:15)
> at Function.Module._load (module.js:474:25)
> at Module.require (module.js:596:17)
> at require (internal/module.js:11:18)
> at Object.<anonymous> (/home/mridul/shafee-it/routes/admin.js:2:18)
> 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> (/home/mridul/shafee-it/server.js:74:21)
> at Module._compile (module.js:652:30)
> at Object.Module._extensions..js (module.js:663:10)
> at Module.load (module.js:565:32) [nodemon] app crashed - waiting for file changes before starting...
Here is my ..models/category(category schema)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
name: { type: String, unique: true, uppercase: true}
});
module.exports = mongoose.model('Category', CategorySchema);
add-category Route in admin.js file
const router = require('express').Router();
const Category = require('..models/category');
// add-category route
router.get('/add-category', function(req, res, next) {
res.render('admin/add-category', { message: req.flash('success') });
});
router.post('/add-category', function(req, res, next) {
var category = new Category();
category.name = req.body.name;
category.save(function(err) {
if (err) return next(err);
req.flash('success', 'Successfully added a category');
return res.redirect('/add-category');
});
});
Here also server.js file to include the category
const Category = require('./models/category');
app.use(function(req, res, next) {
Category.find({}, function(err, categories) {
if (err) return next(err);
res.locals.categories = categories;
next();
});
});
const adminRoutes = require('./routes/admin');
app.use(adminRoutes);
when i run my server or route the add-category url then show module.js:549 throw err; this problem.
Here is my problem details and i check my code different times.
Then how can i solve this problem.
Seems to me that you forgot a /:
require('..models/category');
require('../models/category');
This is why ..models/category throws an error. From a general point of view, when require can't find a local module, don't "check the code", just check where the file is, and what path you wrote, it's always one or the other. ;)
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
am trying to integrate coinpayments into my site am using express js to run it i have gone through the npm docs but it still seems unclear to me and i have tried running some code and still nothing shows up. Any help is highly appreciated.
var express = require("express"),
app = express(),
coinpayments = require("coinpayments"),
bodyparser = require("body-parser")
app.use(bodyParser.urlencoded({extended: true}));
var Coinpayments = require('coinpayments');
var client = new Coinpayments({
key: kfjdkjfkdfkf00d00,
secret: 009093403440349,
});
client.getBasicInfo(function(error,result){
if(error){
console.log(error)
} else{
console.log(result)
}
})
it throws up error in my command line
sniperfillipo:~/workspace/bitcointest/main $ node crypto.js
/home/ubuntu/workspace/bitcointest/main/node_modules/coinpayments/lib/index.js:28
throw new Error('Missing public key and/or secret');
^
Error: Missing public key and/or secret
at new CoinPayments (/home/ubuntu/workspace/bitcointest/main/node_modules/coinpayments/lib/index.js:28:19)
at Object.<anonymous> (/home/ubuntu/workspace/bitcointest/main/crypto.js:235:14)
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:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
Am new to this don't really know for sure how things work
The issue is this section here:
var client = new Coinpayments({
key: kfjdkjfkdfkf00d00, // <-- this line
secret: 009093403440349,
});
What is kfjdkjfkdfkf00d00? It is neither aString nor a Number. It is an undeclared variable.
So you are passing an undeclared variable into the constructor of Coinpayments which takes on the value of undefined judging from the error message you provided.
So your actual constructor looks like:
var client = new Coinpayments({
key: undefined,
secret: 009093403440349,
});
In other words, you need to define your key value.
Im trying to create auto increment field in mongoose but i cant create for some reason
This is my app.js
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Artist = require('./models/artist');
mongoose.connect('mongodb://localhost/auto-increment');
app.get('/', function(req, res){
Artist.create({
name: "artistName",
fpimage: "Cover"
}, function(err, artist){
if(err) {
console.log(err);
} else {
artist.save();
console.log(artist);
res.send('Hi ')
}
});
});
// Set Port
app.set('port', (process.env.PORT || 3000));
// Run Server
app.listen(app.get('port'), function(){
console.log('Server has started on Port: '+app.get('port'));
});
and this is my artist model
var mongoose = require("mongoose");
var autoIncrement = require('mongoose-auto-increment');
var artistSchema = new mongoose.Schema({
name: String,
fpimage: String
});
artistSchema.plugin(autoIncrement, 'Artist');
module.exports = mongoose.model('Artist', artistSchema);
When i run the app, i get following the error message
fn(this, opts);
^
TypeError: fn is not a function
at Schema.plugin (C:\Users\tjesu\Desktop\pidal\auto-increment\node_modules\mongoose\lib\schema.js:1060:3)
at Object.<anonymous> (C:\Users\tjesu\Desktop\pidal\auto-increment\models\artist.js:10:14)
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> (C:\Users\tjesu\Desktop\pidal\auto-increment\app.js:4:14)
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.runMain (module.js:575:10)
How can i fix this error? What should i do?
From the docs, it looks like you're missing two things:
You need to initialize autoIncrement:
autoIncrement.initialize(mongoose.connection);
You need to change your call to:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
use autoIncrement.plugin instead of autoIncrement in your model
like:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
and need initialize autoIncrement once
var connection = mongoose.createConnection("mongodb://localhost/auto-increment");
autoIncrement.initialize(connection);
and can use get like
app.get('/', function(req, res){
var artist = new Artist({name: "Telmuun",fpimage: "Cover"});
artist.save(function(err, saved) {
if(err) {
console.log(err);
}
else {
res.send('saved');
}
});
});
I am trying to run the serverjs in nodejs by separating the routes and models in different folders, but I am getting the below mentioned error where as i can run the same without moving them to different folders.
Error: Cannot find module '../models/catModel'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/rajesh/app4/routes/catRoute.js:2:10)
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 Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/rajesh/app4/cat_server.js:14:12)
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)
Source Code:
/app4/cat_server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/cats');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var cats = require('../routes/catRoute')(app);
var server = app.listen(3000, function(){ console.log('Sever runnign at localhost:3000'); });
/app4/routes/catRoute.js
var _= require('lodash');
var Cat= require('../models/catModel');
module.exports = function(app) {
/*Create*/
app.post('/cat', function(req, res){
var newCat = new Cat(req.body);
newCat.save(function(err){
if(err){ res.json({info: "error during creating of cat create", error:err}); };
res.json({info: 'Cat created successfully'});
});
});
}
/app4/models/catModel.js
var mongoose = require('mongoose');
var catSchema = mongoose.Schema({ name: String, age: Number, type: String });
module.exports = mongoose.model('Cat', catSchema);
Something doesn't look right with the relative path loading.
Given that /app4/routes/catRoute.js is being loaded correctly with a path of ../routes/catRoute, I suspect that doing the following will help:
var Cat = require('../../models/catModel');
This error comes when path is not right for server
Try this
var Cat = require(process.cwd() + '/models/catModel');
Its resolved, the issue is with my catModel.js file has an additional extension, once I updated its working as expected. Thanks for all your help.