小编tht*_*gma的帖子

在多个文件node.js之间共享和修改变量

main.js

var count = 1;

// psuedocode
// if (words typed begins with @add)
require('./add.js');

// if (words typed begins with @remove)
require('./remove.js');

// if (words typed begins with @total)
require('./total.js');



module.exports.count = count;
Run Code Online (Sandbox Code Playgroud)

total.js

var count = require('./main.js').count;
console.log(count);
Run Code Online (Sandbox Code Playgroud)

add.js

var count = require('./main.js').count;
count += 10;
console.log(count);
Run Code Online (Sandbox Code Playgroud)

remove.js

var count = require('./main.js').count;
count -= 10;
console.log(count);
Run Code Online (Sandbox Code Playgroud)

的console.log

 1
 11
 -9
Run Code Online (Sandbox Code Playgroud)

背景:

我有一个应用程序(irc bot),我想添加一个窥视可以执行@add 1或@remove 1的功能.我有一个main.js然后需要不同的文件,具体取决于所说的触发器.因此add会触发add.js文件,然后需要('main.js')并添加10(简化为10,它实际上会解析数字并使用该数字).我遇到的问题是当有人去做并且做@remove时.它需要('main.js')并从1减去10,结果为-9.做@total会输出1.

我已经对module.exports进行了相当不错的搜索,我没有遇到像上面列出的那个例子. 文档中没有任何与我想要做的相近的例子; 而这些问题1,2我的理解-但任何用处我不是-我的理解什么正在那里说.

题:

我想同时让@add和@remove操纵相同的变量(count),并且@total要将@add和@removes考虑在内的总计数返回.我是否正确使用module.exports; 或者是否有共享变量的常用方法,一个文件能够修改module.exports的内容并将结果返回到main.js文件?

variables module sharing node.js total.js

37
推荐指数
4
解决办法
5万
查看次数

nock library - 如何匹配任何url

嗨,我正在尝试nock库,但我正在努力匹配查询字符串上的随机模式.我认为下面的代码应该可以工作,但我无法得到任何工作.

  var nock, request;

  request = require('request');

  nock = require('nock');

  nock("http://www.google.com").filteringPath(/.*/g).get("/").reply(200, "this should work?");

  request("http://www.google.com?value=bob", function(err, res, body) {
    return console.log(body);
  });
Run Code Online (Sandbox Code Playgroud)

node.js

19
推荐指数
3
解决办法
1万
查看次数

使用sparse:true仍然得到MongoError:E11000重复键错误

架构(../models/add.js)

var addSchema = new Schema({
    name: {type: String, unique: true, sparse: true},
    phone: Number,
    email: String,
    country: Number
});

module.exports = mongoose.model('Contact', addSchema);
Run Code Online (Sandbox Code Playgroud)

附加manager.js

var Add = require('../models/add.js');
var AM = {};
var mongoose = require('mongoose');
module.exports = AM;

AM.notOwned = function(country, callback)
{
    Add.update({country: country}, {country: country}, {upsert: true}, function(err, res){
        if (err) callback (err);
        else callback(null, res);
    })
}
Run Code Online (Sandbox Code Playgroud)

news.js

// if country # is not in the database
    AM.notOwned(country, function(error, resp){
        if (error) console.log("error: "+error);
        else …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

5
推荐指数
1
解决办法
1846
查看次数

标签 统计

node.js ×3

module ×1

mongodb ×1

mongoose ×1

sharing ×1

total.js ×1

variables ×1