Reputation: 4352
Node -v 8.9.3
I got my own module, which is part of another module, like this:
module.js
function module(name) {
request(['arr_result1', 'arr_result2', 'arr_result3'], {params, name}) //it's not actual request, I just rename it, 'name' is the same as function
.then(response => {
let data = [];
data.name = response.data.arr_result1;
data.guild = response.data.arr_result2;
data.check_sum = 0;
for (let i = 0; i < 3; i++) {
data.check_sum += adler32.sum(new Buffer((response.data.arr_result3)));
}
return (data);
})
.then(data => {
if (data.arr_result2 === undefined) {
data.arr_result2 = 'TEST';
} else {
data.arr_result2 = data.arr_result2;
}
//other logic
console.log(data);
return data;
})
.catch(function (error) {
console.log(error )
});
}
module.exports = module;
and I'd like to import result ('data' from console.log) of this 'request-promise' to another file (like app.js) via:
app.js
const module = require("./module");
My module works fine, (console.log prints the exact data I want) but I can't access the data, from another *.js via import or require: In this case:
let data = module('name');
console.log(data);
I receive data - underfined (because request is promise and it's not yet fullfilled, am i right?)
but when I try to:
module('name').then((data=> {console.log(data)});
I have error: TypeError: Cannot read property 'then' of undefined
So what am I doing wrong and how should I rewrite it? Actually how it works, I point/define function('name'), and module provide a nessesary data via async request. and then return it to 'back' to app.js P.S.Actually I rename every variable in the code for better interpretation. In my own case it's not a single 'request' and my module have absolutely another name.
UPDATING
Yes, I have alredy tried with
function module(name) {
return request(['arr_result1', 'arr_result2', 'arr_result3'], {params, name})
and 'data' prints fine, but why then this code not working properly?
module('name').then(data => {
console.log(data); //prints succesfully
mongoose_schema-DB.create({data});
});
(node:17964) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: **here is validation error like there is no data at all, but console.log prints it dataset as 'ok'[DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Here is mongoose_schema-DB code:
let mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dma', { useMongoClient: true });
mongoose.Promise = global.Promise;
//schema itself
let mongoose_schema-DB= mongoose.model('mongoose_schema-DB', schema);
module.exports = mongoose_schema-DB;
or this part in Express
app.all('/item', function (req, res, next) {
module('name').then(data => {
mongooseDB_schema-DB.create({data});
});
},function (err, res) {
if (err) return handleError(err);
console.log(res);
});
next();
});
UPDATED
and the most interesting part, this code works fine, instead of simple mongooseDB_schema-DB.create({data})
module(name).then(data => {
mongooseDB_schema-DB.create({
field1: data.1,
field2: data.2,
field3: data.3,
field4: data.4,
field5: data.5,
field6: data.6,
});
});
I'm checking Express verison of it...
I just rewrite express version with callback, instead of .catch. Have no idea how, but it works fine. Thank you all @Bergi and @Keith for providing a nessasry support!
Upvotes: 0
Views: 1094
Reputation: 664548
Yes, it's a promise and you need to wait for it using .then
like you tried, but you also need to return
that promise from your module
function!
function module(name) {
return request(['arr_result1', 'arr_result2', 'arr_result3'], {params, name})
// ^^^^^^
.then(…)
.then(…)
.catch(…);
}
You might also want to omit the catch
and let the caller of your module handle the error, instead of receiving the undefined
handler result.
Upvotes: 2