I have taken reference from few sites
node js topclient
taobao node package
node-taobao-topclient
What I did:
I installed node-taobao-topclient
My code:
TopClient = require('node-taobao-topclient');
const client = new TopClient({
'appkey': 'xxxx',
'appsecret': 'xxxxx',
'REST_URL': 'http://gw.api.taobao.com/router/rest'
});
client.execute('taobao.wlb.imports.general.consign', {
"session" : "620260160ZZ61473fc31270a2c1f5dcc0efdff78b4c58312482635690",
'trade_order_id':'245033103766976',
'resource_id':'5044440108577',
'store_code':'Tran_Store_775585',
'first_logistics':'123',
'first_waybillno':'123',
'sender_id':'228',
'cancel_id':'228'
}, function(error, response) {
if (!error) console.log(response);
else console.log(error);
})
When I run above code I get error :
TypeError: TopClient is not a constructor
As I am new to node I don't how to exactly use such packages as on reference websites they are using it like:
TopClient = require('./topClient').TopClient;
My node-taobao-topclient package looks like below:
Any guidance on how I can use this API in node would be highly appreciated.
I have used for instalation :
npm i taobao-topclient
I have a correct compilation with :
const TopClient = require('taobao-topclient');
const client = new TopClient({
'appkey': aliexpress_app_key,
'appsecret': aliexpress_app_secret,
'REST_URL': 'http://gw.api.taobao.com/router/rest'
});
I have read the library and they do an exports directly from TopClient, so is not necesary to call it again , in the library at the top :
const request = require('request')
const util = require('./topUtil')
module.exports = class TopClient {
constructor (options) {
const opts = options || {}
if (!opts.appkey || !opts.appsecret) {
...............
If you call it again with another point .TopClient, dont found anything.
The following fragament works well:
TopClient = require('node-taobao-topclient').default;
Related
I am new to NodeJS programming so I was trying make simple custom module by extending one module to another which prints in console log.
ExtendMod.js
var exports = module.exports = {};
exports.tutorial = function() {
console.log("N Tutotial");
}
app.js
var Tutor=require('./ExtendMod.js');
exports.NodeTutorial = function() {
console.log("Node Tutorial")
function pTutor() {
var PTutor=Tutor
PTutor.tutorial;
}
}
console.log(require('./thirdNode.js').NodeTutorial());
I have extended ExtendMod.js in app.js.
Question: Why an empty braces after exports when we create my first module and also in app.js we didn't take exports as a variable is it global?
My output :
Node Tutorial
N Tutorial
Undefined
What is this undefined?
I work on a server in node.js, and i'm quite new to the concept.
I'm using socket.io to communicate with clients
I have many modules in my server and i try to send a function declared in a module to my main. I don't want to use exports.plugin=plugin because I try to work with the observer pattern.
I tried :
In main.js:
//Modules NPM
var express = require('express');
var observer = require('node-observer');
//Extensions JS
var mymodule = require('./mymodule');
var plugin = require('./plugin');
//Initialisation
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
socket.on('message', function (var1, var2) { //Come from client
socket.var1=var1;
socket.var2=var2;
observer.send(this, "Message", "varSpecification");
});
In plugin.js :
//Modules NPM
var express = require('express');
var observer = require('node-observer');
//Extensions JS
var mymodule = require('./mymodule');
var main = require("./main");
...
observer.subscribe(this, "Message", function(who, data) {
var functionToSend= new Function ("varA", "varB" ,"mymodule.function(varA, varB); socket.emit('messageOK');"); //I can't create a real function because i don't use socket.io in the module Plugin
observer.send(this, "Response", functionToSend); });
Back to the main:
observer.subscribe(this, "Response", function(who, functionReceived) {
functionReceived(socket.var1, socket.var2); //Doesn't work
});
The problem is that when I execute the function in main.js, it doesn't find mymodule and can't execute the function associated.
(ReferenceError : mymodule is not defined)
When i execute the function directly in the main.js, it works.
observer.subscribe(this, "Response", function(who, functionReceived) {
mymodule.function(socket.var1, socket.var2); //Works
});
So I guess it's a problem with the object that I send
I will be more than grateful if you can help me!
EDIT : After other tests I found the source of the problem :
var mymodule = require('./mymodule');
...
mymodule.function(a, b); //works
var functionTest= new Function('mymodule.function(a, b)');
functionTest(); //Doesn't work (mymodule is not defined)
I still want to use the second option for my project but don't know how
I used the function constructor, and i didn't know that the function is declared in the global scope.
So i had to write this to make it work :
var functionTest= new Function('mymodule', 'a', 'b','mymodule.function(a, b)');
functionTest(mymodule,a,b);
I had to pass mymodule, a and b in parameter
I'm requiring a library in NodeJS which has a self-invoking function, that results an error because it looks for an object which is not initialized at that moment .
I want to dynamically require this library when that object is initialized.
Is there any way to dynamically require/ load a library ?
This is the part of library required :
https://github.com/sakren/node-google-maps/blob/develop/lib/Google.js#L5
Actually I want to require when the window object is present (client-side rendering).
So something like this :
'use strict';
var React = require('react');
var Map = require('./map.jsx');
var Common = require('../common/common');
var MapStatic = require('./map-static.jsx');
exports.type = function() {
return 'map';
};
exports.jsx = function(data) {
if (Common.isServerSide()) {
return (<MapStatic data={data}/>);
} else {
return (
<Map data={data}/>
);
}
};
exports.transform = require('./map-transform.js');
The reason the code looks weired is that I'm using react.
In nodeJS require can be used anywhere at anytime whithout much limitations AFAIK.
Which error is thrown once you require at runtime ?
In your else branch.
Try the following.
requires = {}
function getter(key) {
if(!requires[key]){
requires[key] = require(key)
}
return requires[key]
}
I am using nodejs in my meteor app and I added packages using mrt add npm and then in my client directory in packages.json I added skimlinksjs and its version and it is added to my app.
When I tried to using them in my app in server side code like this,
var res;
var skim = Meteor.require('skimlinksjs');
var apili = Meteor.require('/config.js');
skim.setup(apili.key);
skim.query({
searchFor: "title:\"moto g\"",
fq: "country:US"
}, function(err,data) {
res=data.skimlinksProductAPI.numFound;
}
);
return res;
and my config.js file is like this
module.exports = {
key: "xxxxxxx"
}
whenI'm running this application it is showing error like
module not defined
What went wrong with my code or is there any need to install other packages?
I just got the answer
Write this function in server side code
function returnAllResult()
{
var skimlinks = Meteor.require('skimlinksjs');
skimlinks.setup("xxx");
var skimlinks_query = Async.wrap(skimlinks.query);
var result = skimlinks_query({
searchFor: "title:\"moto g\"",
fq: "country:US",
rows:5
});
return result;
}
to know about asynchronous functions watch this
and then in my server side methods call this
apiresult:function()
{
var response = returnAllResult();
return response.skimlinksProductAPI.products[0].merchant;
}
that's it working fine now. Hope this helps someone
I have a Locations model and a Recorders model. I want to be able to pass all of the data for both data sets to my view model. How can I access them though because I think they're not in scope since I'm getting undefined errors because I'm calling 'all'
https://gist.github.com/3998302
var Main = function () {
this.index = function (req, resp, params) {
var self = this;
var data = {};
geddy.model.Locations.all(function(err, locations) {
data.locations = locations;
geddy.model.Recorders.all(function(err, recorders) {
data.recorders = recorders;
self.respond({params: params, data: data}, {
format: 'html'
, template: 'app/views/locations/index'
}
});
}););
};
};
exports.Main = Main;
Error snippet:
timers.js:103
if (!process.listeners('uncaughtException').length) throw e;
^
TypeError: Cannot call method 'all' of undefined
at index (G:\code\PeopleTracker\app\controllers\main.js:23:24)
at controller.BaseController._handleAction.callback (C:\Users\Chris\AppData\Roaming\npm\node_modules\geddy\lib\base_
controller.js:387:22)
So it looks like you're initializing the data variable to 'undefined'. Try data = {} instead. If that doesn't fix it, I'll do some troubleshooting with you.
EDIT
If that doesn't do it for you, try installing geddy again:
npm uninstall -g geddy && npm install -g geddy
If that doesn't do it, make sure that your DB is actually running, make sure that the models are defined (try geddy console to check your models), and make sure that you're on the latest stable version of node.
Very late to the party, but I believe you can just call
geddy.model.Locations.all(function(err, locations) {
geddy.model.Recorders.all(function(err, recorders) {
var data = {};
data.locations = locations;
data.recorders = recorders;
self.respond({params: params, data: data}, {
format: 'html'
, template: 'app/views/locations/index'
}
});
}););
You could also have the respond say
self.respond({params: params, locations: locations, recorders: recorders});
but if you want all of that data available from the data literal you need it defined in the lowest scope callback. The callbacks can read above their scope but they cannot write above it.