新手请教 Nodejs require 使用问题

新手请教 Nodejs require 使用问题

模块引用的时候报 …/xxxx/xxx has no method ‘add’

Dao var DB=require(’…/DBConfig/DBConfig’);

exports.add=function(DBModel,collname) { DB.open(function(err){ DB.collection(collname,function(err,collection){ collection.insert(DBModel,function(err,doc){ DB.close(); }); }); }); }; 引用 var CommonDao=(’…/Common/Dao’); CommonDao.add(userModel,‘user’);


5 回复

当然可以!让我们来详细分析一下你遇到的 require 使用问题,并提供一些示例代码来帮助你理解。

问题分析

从你的描述来看,错误信息是 …/xxxx/xxx has no method 'add'。这意味着你在尝试调用 add 方法时,该方法并不存在于你所引用的对象中。这通常是因为 require 的路径不正确或者模块导出的方式不正确。

示例代码及解释

1. 检查路径是否正确

确保 require 的路径是正确的。你当前使用的是相对路径 …/DBConfig/DBConfig…/Common/Dao,这可能是一个拼写错误或路径配置错误。假设你的文件结构如下:

project-root/
├── Common/
│   └── Dao.js
├── DBConfig/
│   └── DBConfig.js
└── index.js

你应该使用如下路径:

// 在 Dao.js 中
var DB = require('../DBConfig/DBConfig');

exports.add = function(DBModel, collname) {
    DB.open(function(err) {
        if (err) throw err;
        DB.collection(collname, function(err, collection) {
            if (err) throw err;
            collection.insert(DBModel, function(err, doc) {
                if (err) throw err;
                DB.close();
            });
        });
    });
};

// 在 index.js 中
var CommonDao = require('./Common/Dao');
var userModel = { /* 你的用户模型数据 */ };

CommonDao.add(userModel, 'user');

2. 检查导出方式是否正确

确保你在 Dao.js 文件中正确地导出了 add 函数。上面的代码已经展示了如何正确地导出函数。

3. 确保 DBConfig.js 文件也正确导出

假设 DBConfig.js 是一个数据库配置文件,确保它也正确地导出了所需的模块。例如:

// DBConfig.js
module.exports = {
    open: function(callback) {
        // 打开数据库连接的逻辑
    },
    collection: function(collname, callback) {
        // 获取集合的逻辑
    },
    close: function() {
        // 关闭数据库连接的逻辑
    }
};

总结

通过检查路径和导出方式,你可以解决 require 使用中的问题。确保路径正确并且导出方式符合预期。希望这些示例代码和解释能帮助你解决问题。如果你有更多具体的问题或需要进一步的帮助,请随时提问!


仔细检查下自己的代码吧, 至少这句话var CommonDao=('../Common/Dao');不正确吧? 你是想表达require的意图吗?

CommonDao 就是个 string

> var CommonDao=('../Common/Dao');
undefined
> CommonDao
'../Common/Dao'
> typeof CommonDao
'string'

var CommonDao=(’…/Common/Dao’); 是这里的问题。 缺少require。

大意了! 谢谢

根据你的描述,错误信息 .../xxxx/xxx has no method 'add' 表明在调用 CommonDao.add 方法时,它并不存在或没有被正确导出。通常这种问题可能是由于文件路径错误、模块导出方式不正确或者方法定义的位置不正确导致的。

示例代码及说明

假设目录结构如下:

project/
├── Common/
│   └── Dao.js
├── DBConfig/
│   └── DBConfig.js
└── index.js

Dao.js 文件

确保你在 Dao.js 中正确地导出了 add 方法:

// Common/Dao.js
var DB = require('../DBConfig/DBConfig');

exports.add = function(DBModel, collname) {
    DB.open(function(err) {
        if (err) throw err;
        
        DB.collection(collname, function(err, collection) {
            if (err) throw err;
            
            collection.insert(DBModel, function(err, doc) {
                if (err) throw err;
                
                DB.close();
            });
        });
    });
};

index.js 文件

确保你在 index.js 中正确地引入了 Dao 模块,并且调用了 add 方法:

// index.js
var CommonDao = require('./Common/Dao');
var userModel = { /* 你的用户模型数据 */ };

CommonDao.add(userModel, 'user');

常见问题及解决方案

  1. 路径错误

    • 确保使用正确的路径来导入模块。相对路径应以 ./../ 开始。
  2. 导出方式不正确

    • Dao.js 中使用 exports 正确地导出 add 方法。
  3. 函数执行顺序

    • DB.open 是一个异步操作,确保在操作数据库之前它已经成功完成。

通过上述步骤,你应该能够解决 no method 'add' 的错误。如果还有其他问题,请检查是否有其他错误输出或异常信息。

回到顶部