问题描述
这是我的server.js文件和api.js文件.我在尝试根据其属性搜索js对象的排序函数中遇到错误.事件模式具有名称,位置,价格,等级等属性.我试图根据他们的价格对其进行排序.
This is my server.js file and api.js file. I am getting an error in the sort function in which I intend to search the js objects according to their attributes.The event Schema has the attributes as name, location, price, rating. I tried to sort it according to their prices.
server.js
var express= require('express');
var bodyParser= require('body-parser');
var morgan = require('morgan');
var config=require('./config');
var app= express();
var mongoose=require('mongoose');
//var User=require('./database/user')
mongoose.connect('mongodb://localhost:27017/db',function(err){
if(err){
console.log(err);
}
else{
console.log("connected!");
}
});
app.use(bodyParser.urlencoded({extended: true })); //if false then parse only strings
app.use(bodyParser.json());
app.use(morgan('dev'));//log all the requests to the console
var api=require('./app/routes/api')(app,express);
app.use('/api',api);
app.get('*',function(req,res){
res.sendFile(__dirname + '/public/views/index.html');
}); // * means any route
app.listen(config.port,function(err){
if(err){enter code here
console.log(err);
}
else{
console.log("The server is running");
}
});
module.exports = router;
api.js
var User = require('../models/user');
var Event = require('../models/event');
var config = require('../../config');
var secret = config.secretKey;
module.exports = function (app, express) {
var api = express.Router();
app.use()
api.post('/signup', function (req, res) {
var user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password
});
user.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'User created!'
});
});
});
api.get('/users', function (req, res) {
User.find({}, function (err, users) {
if (err) {
res.send(err);
return;
}
res.json(users);
});
});
api.post('/eventfeed', function (req, res) {
var event = new Event({
name: req.body.name,
location: req.body.location,
description: req.body.description,
price: req.body.price,
rating: req.body.rating
});
event.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'Event created!'
});
});
});
// utility function for sorting an array by a key in alpha order
api.get('/sortby_price', function (err) {
if (err) return err;
// utility function for sorting an array by a key in parsed numeric order
else {
function sortArrayNum(arr, key) {
arr.sort(function (a, b) {
return parseInt(a[key], 10) - parseInt(b[key], 10);
});
}
var dicts = EventSchema.saved;
for (var i = 0; i < dicts.length; i++) {
var terms = dicts[i].terms;
sortArrayNum(terms, "price");
}
}
return api;
});
}
这是我的错误.我是第一次使用此网页.请帮我这个错误告诉你什么.
This is my error. I am making a webpage for the first time using this. Kindly help me what does this error tells.
推荐答案
我被排除在外时遇到了这个问题
I had this problem when I left out
module.exports = router;
在我的Routes.js中.我们需要导出所有路由.
in my Routes.js.We need to export all the routes.
在我的server.js中,
In my server.js, I had
var mainRoutes = require('./Routes.js')
app.use(mainRoutes)
因此,请检查您的"app/routes/api"文件,查看其是否具有正确的导出功能.
So check your 'app/routes/api' file to see if it has proper export.
这篇关于TypeError:app.use()需要中间件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!