gpt4 book ai didi

javascript - "TypeError: Cannot read property ' 申请 ' of undefined"

转载 作者:IT老高 更新时间:2023-10-28 23:20:55 25 4
gpt4 key购买 nike

使用 node、express、socket.io、jade 和 angular。收到错误:TypeError: Cannot read property 'apply' of undefined。有什么建议吗?

index.js:

module.exports = function(app, res) {
res.render('index', { title: 'Express' });
var io = app.get('io');
io.on('connection', function(socket){
});
};

index.jade:

extends layout

block content

script.
var app = angular.module('hackigur', []);
var socket = io.connect();
var refreshTimer = 10;

app.controller('UpdateController', function($scope){
//socket.on('update', function(msg){
//$scope.refreshTimer = msg;
//$scope.$apply();
//});

setInterval(secondTick,1000);

function secondTick() {
if(refreshTimer != 0) {
refreshTimer -= 1;
}
$scope.refreshTimer = refreshTimer;
$scope.$apply();
};
});

h1= title
p Welcome to #{title}

div(ng-controller="UpdateController")
p(ng-bind="refreshTimer")

layout.jade:

doctype htmlhtml(ng-app="hackigur")  head    title= title    script(src = "/socket.io/socket.io.js")    script(src = "/js/angular/angular.min.js")  body    block content

完全错误:

Server listening on port 3000TypeError: Cannot read property 'apply' of undefined    at Server.(anonymous function) [as on] (D:\Projects\hackigur\node_modules\socket.io\lib\index.js:364:15)    at module.exports (D:\Projects\hackigur\server\api\index.js:30:8) at ...

最佳答案

调用我的 index.jsroutermodule.export 中传递了 app,如下所示:

module.export = function (app) {
app.get( ... , function(..., res) {
require(index.js)(app)(res);
};

我需要在 module.export 外部为 app 声明一个变量:

var x;
module.export = function (app) {
x = app;
app.get( ... , function(..., res) {
require(index.js)(x)(res);
};

不完全理解它为什么起作用,但它似乎通过应用上述方法将正确的 app 对象传递给 app.get

关于javascript - "TypeError: Cannot read property ' 申请 ' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012099/

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