我是node.js.My express.js应用程序结构的新成员:
| my-application
| -- app.js
| -- node_modules
| -- public
| -- models
| -- users.js
| -- routes
| -- index.js
| -- views
| -- index.ejs
我尝试使用$ .inArray()函数。我安装了https://www.npmjs.com/package/jquery包。
路由/ index.js
var express = require('express');
var $ = require('jquery');
var router = express.Router();
router.get('/', function(req, res, next) {
var arr = [ 'a','b','c' ];
$.inArray('a',arr);
res.render('index',{
title:'home'
});
});
module.exports = router;
它给了我以下错误:
undefined is not a function
TypeError: undefined is not a function
at c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\routes\index.js:12:7
at Layer.handle [as handle_request] (c:\Users\Ahmed Dinar\Dropbox \IdeaProjects\justoj\node_modules\express\lib\router\layer.js:95:5)
at next (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\layer.js:95:5)
at c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\index.js:277:22
at Function.process_params (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\index.js:330:12)
at next (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\index.js:271:10)
at Function.handle (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\index.js:176:3)
at router (c:\Users\Ahmed Dinar\Dropbox\IdeaProjects\justoj\node_modules\express\lib\router\index.js:46:12)
我如何在模型中的路径中使用jQuery函数。
提前致谢。
答案 0 :(得分:1)
jQuery与Node JS不兼容,我建议使用Lodash - https://lodash.com/。
var express = require('express');
var _ = require('lodash');
var router = express.Router();
router.get('/', function(req, res, next) {
var arr = [ 'a','b','c' ];
var inArray = _.indexOf('a',arr) > 0;
res.render('index',{
title:'home'
});
});
module.exports = router;
答案 1 :(得分:0)
请记住,jquery需要一个文档窗口才能正常工作 您可以使用 jsdom :
var jsdom = require("jsdom"),
$ = require('jquery')(jsdom.jsdom().parentWindow);