JavaScript

Topic: Anything JavaScript, ECMAScript including Node, React, ...
Nov 8, 2019 12:50
:P
Nov 8, 2019 12:50
see this thing on MONDAY
Nov 8, 2019 12:50
I have to leave now
Nov 8, 2019 12:49
but I don't know how
Nov 8, 2019 12:49
I have to use promise
Nov 8, 2019 12:49
but there is problem with promise
Nov 8, 2019 12:49
doc is printing fine
Nov 8, 2019 12:48
fine
Nov 8, 2019 12:48
here doc is printing
Nov 8, 2019 12:47
const {Platform} = require('../models/platform');

module.exports = {
    getTrades: function(id,userId) {
        Platform.findOne({platform: id, userID: userId}, (err,doc) => {
            if(err) return res.status(400).send(err);
                console.log(doc)
             return (doc);
        })
    },
    multiply: function(a,b) {
        return a*b
    }
};
Nov 8, 2019 12:47
it works fine
Nov 8, 2019 12:46
module.exports = {
    getTrades: function(id,userId) {
       return id;
    },
    multiply: function(a,b) {
        return a*b
    }
};
Nov 8, 2019 12:46
what I mean
Nov 8, 2019 12:46
let me tell you
Nov 8, 2019 12:45
Platform.findOne is right
Nov 8, 2019 12:44
return
Nov 8, 2019 12:44
because it goes to next line and print undfind it does'nt wait for the function
Nov 8, 2019 12:42
it logs undefined
Nov 8, 2019 12:42
this is first file

 const trades = functions.getTrades(id,userId)
        console.log(trades);
Nov 8, 2019 12:41
it returns undefined
Nov 8, 2019 12:41
@ne
Nov 8, 2019 12:37
added .then and catch
Nov 8, 2019 12:37
still undefined
Nov 8, 2019 12:37
Platform.findOne({platform: id, userID: userId}).then(doc => doc).catch(err => err);
Nov 8, 2019 12:36
let me do findOne
Nov 8, 2019 12:35
how can I do that?
Nov 8, 2019 12:34
thats why it is undefined
Nov 8, 2019 12:34
it means it goes to next line. did not wait for data which is coming from other file
Nov 8, 2019 12:33
it never returns undefined
Nov 8, 2019 12:33
mongoose query
Nov 8, 2019 12:33
when I don't user
Nov 8, 2019 12:33
nope I have checked it returns perfect document
Nov 8, 2019 12:30
but still it returns undefined
Nov 8, 2019 12:29
module.exports = {
    getTrades: async function(id,userId) {
        await Platform.findOne({platform: id, userID: userId}).then(doc => doc).catch(err => err);
    },
    multiply: function(a,b) {
        return a*b
    }
};
Nov 8, 2019 12:29
oh okay
Nov 8, 2019 12:28
I have also added async and await
Nov 8, 2019 12:28
I have two files
Nov 8, 2019 12:27
but how can I use promise? in other file?
Nov 8, 2019 12:27
there must be promise
Nov 8, 2019 12:27
yes
Nov 8, 2019 12:27
its return everything fine
Nov 8, 2019 12:27
document is good
Nov 8, 2019 12:27
No
Nov 8, 2019 12:21
how can I achive this?
Nov 8, 2019 12:21
trades is undefined
Nov 8, 2019 12:21
module.exports = {
getTrades: function(id,userId) {
Platform.findOne({platform: id, userID: userId}, (err,doc) => {
if(err) return res.status(400).send(err);
return (doc);
})
},
multiply: function(a,b) {
return a*b
}
};
Nov 8, 2019 12:21
second file:
Nov 8, 2019 12:20
main.js I have function like this importing from other file const trades = functions.getTrades(id,userId);
Nov 8, 2019 12:19
Hi anybody there need help in nodejs
 
Oct 31, 2019 08:26
just use ****** for posting questions thanks