gpt4 book ai didi

javascript - 如何在无服务器框架中使用 jquery?

转载 作者:行者123 更新时间:2023-11-30 15:09:43 25 4
gpt4 key购买 nike

我想在我的无服务器 lambda 项目中使用 jquery 延迟方法。但是,在需要 jquery 和 jquery-ui 作为依赖项之后,当我尝试使用 jquery 库时会出现以下错误?我是无服务器和 lambda 的新手,所以请参阅下面的 handler.js 函数。

TypeError: $.each is not a function
at /vagrant/project/node_modules/jquery-ui/ui/widget.js:690:3
at widgetUuid (/vagrant/project/node_modules/jquery-ui/ui/widget.js:24:3)
at Object.<anonymous> (/vagrant/project/node_modules/jquery-ui/ui/widget.js:26:2)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/vagrant/project/handler.js:4:19)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:156:33)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
at AwsInvokeLocal.tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)

处理程序.js

'use strict';

global.jQuery = require('jquery');
global.jQueryUI = require('jquery-ui');

var Model = require('./resources/model');

module.exports.project = function(event, context, callback) {
Model.init();
};

模型.js

var JsonFile = require('jsonfile');
var $ = global.jQueryUI;

module.exports = {
init : function() {
var self = this;

self.fetch_file().done(function(file){
console.log(file);
});
},

fetch_file : function(){
var deferred = $.Deferred();
JsonFile.readFile('path to file', function (err, file) {
deferred.resolve(file);
});

return $.when(deferred).promise();
}
};

包.json

{
"private": true,
"dependencies": {
"avro-js": "^1.8.2",
"aws-sdk": "^2.88.0",
"jquery": "^3.2.1",
"jquery-ui": "^1.12.1",
"jsonfile": "^3.0.1"
},
"name": "project",
"version": "0.1.0"
}

在需要 Jquery 而不是 JqueryUI 之后,我得到了这个异常:

    TypeError: $.Deferred is not a function
at Object.fetch_schema (/vagrant/project/resources/model.js:17:26)
at Object.init (/vagrant/project/resources/model.js:11:14)
at module.exports.pixel_event_lambda_producer (/vagrant/project/handler.js:9:10)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:229:12)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
From previous event:
at Object.invoke:local:invoke [as hook] (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:24:10)
at BbPromise.reduce (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:55)
From previous event:
at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:22)
at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:237:17)
at variables.populateService.then (/usr/local/lib/node_modules/serverless/lib/Serverless.js:99:33)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:86:74)
at serverless.init.then (/usr/local/lib/node_modules/serverless/bin/serverless:39:50)
at <anonymous>

最佳答案

当我特别需要可以在这里找到的 jquery-deferred 模块时,问题得到解决:https://www.npmjs.com/package/jquery-deferred

关于javascript - 如何在无服务器框架中使用 jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279437/

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