gpt4 book ai didi

javascript - jQuery 函数不适用于 node.js 中的 express.js 路由

转载 作者:搜寻专家 更新时间:2023-11-01 00:00:57 24 4
gpt4 key购买 nike

我是 node.js 的新手。我的 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包。

routes/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 函数。

提前致谢。

最佳答案

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;

关于javascript - jQuery 函数不适用于 node.js 中的 express.js 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33185776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com