2017-03-20 107 views
0

我有两个类在同一个文件夹名为controllers.js和utile.js。 Utile.js是一类客户端。这是utile.js:我得到错误“路径必须是字符串”

'use strict'; 

define('utile', [], function() { 
    var modulo = {}; 

modulo.getLimiteCaratterePost = function() { 
     var limite_carattari_post = 25; 
     return limite_carattari_post; 
    }; 
return modulo; 
}); 
在我controllers.js

我需要用这种方法,所以我尝试以这种方式导入这个类:

async.waterfall([ 
      function(next){ 
       //the error start from the above line 
       require(['utile'], function (utile) { 
        console.log("uitle dentro "+JSON.stringify(utile_metodo.getLimiteCaratterePost())); 
        return next(); 
       }); 

      }, 
      function(next) { 
       console.log("GET LIMITE CARATTER "+JSON.stringify(utile_metodo.getLimiteCaratterePost())); 
       db.getObject(hash + ":bookmark:" + id_bookmark, next); 
      }, 
...... (continue the problem is the import); 

该计划为它我:

/bookmark/5/aaa 
AssertionError: path must be a string 
    at Module.require (module.js:496:3) 
    at require (internal/module.js:20:19) 
    at /vagrant/nodebb/node_modules/nodebb-plugin-connect-bookmarked/lib/controllers.js:34:5 

任何人都可以帮到我吗?

+0

你可以粘贴你的require.config主文件吗? – ayxos

+0

@ayxos require.config({ \t的baseUrl:config.relative_path + “/ SRC /模块”, \t waitSeconds:7, \t urlArgs: “V =” +配置[ '缓存无效'], \t路径:{ \t \t '论坛': '../client', \t \t '管理员': '../admin', \t \t '供应商':' ../../vendor”, \t \t 'plugins':'../../plugins' \t} }); – Picco

+0

我的文件夹不在“baseUrl”路径中!我怎样才能修改这个? – Picco

回答

0

首先,当您创建模块时,您必须使用module.exports对象将其导出,方法是将对象添加到该对象中。例如module.exports= {method: myMethod}等等

然后如果你想使用模块,你必须要求它使用require(path)其中路径可以是一个相对路径,并且必须是一个字符串。所以,如果你想要模块,你可以用exapmle var utile = require('./utile.js')

+0

如果我corret我的代码以您的解决方案时,在controllers.js调用的方法: “utile_metodo =需要(” ./ utile.js'); \t \t \t \t的console.log(” PATH“+ utile_metodo.getLimiteCaratterePost( ));“我otbain错误:”定义未定义“在utile.js – Picco

+0

尝试导出模块,通过添加到'module.exports'您的对象。 – Oskar

+0

它给我的“模块不定义”的问题是这个utile.js有一些方法,我从客户端使用。但我需要在同一个文件夹中的controllers.js中使用一些方法。在客户端文件中我需要(['utile'],函数(utile){}“),它可以工作,但是当我在我的controllers.js中执行的非客户端文件时,它不起作用! – Picco

相关问题