Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us

Kenil Vasani

Ask Kenil Vasani
0Followers
646Questions
Home/ Kenil Vasani/Answers
  • About
  • Questions
  • Polls
  • Answers
  • Best Answers
  • Followed Questions
  • Favorite Questions
  • Groups
  1. Asked: December 22, 2020

    Mongoose: how to use aggregate and find together

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    For MongoDB 3.6 and greater, use the $expr operator which allows the use of aggregation expressions within the query language: var followers_count = 30; db.locations.find({ "$expr": { "$and": [ { "$eq": ["$name", "development"] }, { "$gte": [{ "$size": "$followers" }, followers_count ]} ] } }); ForRead more

    For MongoDB 3.6 and greater, use the $expr operator which allows the use of aggregation expressions within the query language:

    var followers_count = 30;
    db.locations.find({
       "$expr": { 
           "$and": [
               { "$eq": ["$name", "development"] },
               { "$gte": [{ "$size": "$followers" }, followers_count ]}
           ]
        }
    });
    

    For non-compatible versions, you can use both the $match and $redact pipelines to query your collection. For example, if you want to query the locations collection where the name is ‘development’ and followers_count is greater than 30, run the following aggregate operation:

    const followers_count = 30;
    Locations.aggregate([
        { "$match": { "name": "development" } },
        {
            "$redact": {
                "$cond": [
                    { "$gte": [ { "$size": "$followers" }, followers_count ] },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        }
    ]).exec((err, locations) => {
        if (err) throw err;
        console.log(locations);
    })
    

    or within a single pipeline as

    Locations.aggregate([
        {
            "$redact": {
                "$cond": [
                    { 
                        "$and": [
                            { "$eq": ["$name", "development"] },
                            { "$gte": [ { "$size": "$followers" }, followers_count ] }
                         ]
                    },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        }
    ]).exec((err, locations) => {
        if (err) throw err;
        console.log(locations);
    })
    

    The above will return the locations with just the _id references from the users. To return the users documents as means to “populate” the followers array, you can then append the $lookup pipeline.


    If the underlying Mongo server version is 3.4 and newer, you can run the pipeline as

    let followers_count = 30;
    Locations.aggregate([
        { "$match": { "name": "development" } },
        {
            "$redact": {
                "$cond": [
                    { "$gte": [ { "$size": "$followers" }, followers_count ] },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        },
        {
            "$lookup": {
                "from": "users",
                "localField": "followers",
                "foreignField": "_id",
                "as": "followers"
            }
        }
    ]).exec((err, locations) => {
        if (err) throw err;
        console.log(locations);
    })
    

    else you would need to $unwind the followers array before applying $lookup and then regroup with $group pipeline after that:

    let followers_count = 30;
    Locations.aggregate([
        { "$match": { "name": "development" } },
        {
            "$redact": {
                "$cond": [
                    { "$gte": [ { "$size": "$followers" }, followers_count ] },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        },
        { "$unwind": "$followers" },
        {
            "$lookup": {
                "from": "users",
                "localField": "followers",
                "foreignField": "_id",
                "as": "follower"
            }
        },
        { "$unwind": "$follower" },
        {
            "$group": {
                "_id": "$_id",
                "created": { "$first": "$created" },
                "name": { "$first": "$name" },
                "followers": { "$push": "$follower" }
            }
        }
    ]).exec((err, locations) => {
        if (err) throw err;
        console.log(locations);
    })
    
    See less
    • 8
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  2. Asked: December 22, 2020

    Npm ERR! code EPERM

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    Are you using any private or company registry. check your .npmrc file and make sure its available. if you are in company environment, make sure proxy is not a problem. check in your folder if any package-lock.json is generated. you can delete that make re install. I have little doubt but node 6.x.xRead more

    Are you using any private or company registry. check your .npmrc file and make sure its available.

    if you are in company environment, make sure proxy is not a problem.

    check in your folder if any package-lock.json is generated. you can delete that make re install.

    I have little doubt but node 6.x.x with npm 5.x.x might be a problem as node 7.x.x is compatible arable with 5.x.x. And normally node 6.x.x is compatible with npm 4.x.x

    See less
    • 4
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  3. Asked: December 22, 2020In: Javascript

    Error: File to import not found or unreadable: ~bootstrap/scss/bootstrap

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    When Sass is precompiled by its own CLI, it processes @imports by itself, and sometimes thus doesn’t understand ~ notation. So you can import "node_modules/bootstrap/scss/bootstrap"; in first place and replaced the ~ notation with node_modules/ instead.

    When Sass is precompiled by its own CLI, it processes @imports by itself, and sometimes thus doesn’t understand ~ notation. So you can import "node_modules/bootstrap/scss/bootstrap"; in first place and replaced the ~
    notation with node_modules/ instead.

    See less
    • 9
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  4. Asked: December 22, 2020In: Javascript

    How to console.log an error with stack trace in node.js?

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    I've just worked out what was going on, and I hope this will help others to avoid this beginner's error. For some of my error logging I was using something like the following, using string concatenation to construct the error message: console.log('error in function abc: ' + err + ' whilst doing xyz'Read more

    I’ve just worked out what was going on, and I hope this will help others to avoid this beginner’s error.

    For some of my error logging I was using something like the following, using string concatenation to construct the error message:

    console.log('error in function abc: ' + err + ' whilst doing xyz');
    

    whereas elsewhere I was using something like the following, just passing the pieces of the error message as separate arguments to console.log:

    console.log('error in function xyz:', err, 'whilst doing abc');
    

    I now see that these give different results!

    The former must stringify err so that it can be concatenated with the other parts of the message, and according to this, in doing so it just uses the message part.

    However, in the latter form the err object must be processed by console.log unadulterated, and dumped as a whole.

    This explains why I was sometimes not seeing the whole content of the error, as I was expecting, and other times I was.

    As for console log messages put there by other libraries, something else to check is that you’re not filtering out the ‘stack’ parts of the log messages in your log viewer… turns out that I was (in order to save on log quota… am using papertrail)… d’oh. I was doing so by filtering out any lines starting with ____at (four spaces followed by ‘at’), for example ____at Request.self.callback.

    See less
    • 2
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  5. Asked: December 22, 2020In: Javascript

    Koa router: How to get query string params?

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    According to the docs there should be a ctx.request.query that is the query string items represented as an object.

    According to the docs there should be a ctx.request.query that is the query string items represented as an object.

    See less
    • 8
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  6. Asked: December 22, 2020

    ERROR in ./node_modules/css-loader?

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    try using npm rebuild node-sass

    try using

    npm rebuild node-sass

    See less
    • 4
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  7. Asked: December 22, 2020

    node.js: how to use setInterval and clearInterval?

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    Using setInterval() What if you need to repeat the execution of your code block at specified intervals? For this, Node has methods called setInterval() and clearInterval(). The setInterval() function is very much like setTimeout(), using the same parameters such as the callback function, delay, andRead more

    Using setInterval()

    What if you need to repeat the execution of your code block at specified intervals? For this, Node has methods called setInterval() and clearInterval(). The setInterval() function is very much like setTimeout(), using the same parameters such as the callback function, delay, and any optional arguments for passing to the callback function.

    A simple example of setInterval() appears below:

    var interval = setInterval(function(str1, str2) {
      console.log(str1 + " " + str2);
    }, 1000, "Hello.", "How are you?");
    
    clearInterval(interval);
    

    This is another way when you want to keep only one interval running every minute

    function intervalFunc() {
        console.log("Hello!!!!");
         }
        setInterval(intervalFunc,1500);
    

    In the above example, intervalFunc() will execute about every 1500 milliseconds, or 1.5 seconds, until it is stopped . Hope this helps.

    See less
    • 1
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  8. Asked: December 22, 2020In: Javascript

    TypeError: res.json is not a function

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    Because you're overwriting your res variable in the .then of your rp function: app.post('/danger', function response(req, res) { //see, "res" here was being overwritten .. .. rp(option).then(function(response) { //change the variable name of "res" to "response" (or "turtles", who cares, just dont ovRead more

    Because you’re overwriting your res variable in the .then of your rp function:

    app.post('/danger', function response(req, res) { //see, "res" here was being overwritten
       ..
       ..
       rp(option).then(function(response) { //change the variable name of "res" to "response" (or "turtles", who cares, just dont overwrite your up most "res")
    
    See less
    • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  9. Asked: December 22, 2020In: Javascript, ReactJs

    ‘Access-Control-Allow-Origin’ issue when API call made from React (Isomorphic app)

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    CORS is a browser feature. Servers need to opt into CORS to allow browsers to bypass same-origin policy. Your server would not have that same restriction and be able to make requests to any server with a public API. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Create an endpRead more

    CORS is a browser feature. Servers need to opt into CORS to allow browsers to bypass same-origin policy. Your server would not have that same restriction and be able to make requests to any server with a public API. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

    Create an endpoint on your server with CORS enabled that can act as a proxy for your web app.

    See less
    • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  10. Asked: December 22, 2020

    Git Bash Command on Windows, yarn command not found

    Kenil Vasani

    Kenil Vasani

    • 646 Questions
    • 567 Answers
    • 77 Best Answers
    • 26 Points
    View Profile
    Best Answer
    Kenil Vasani
    Added an answer on December 22, 2020 at 7:07 am

    You probably just did not install yarn yet. Have a look at the yarn installation for Windows here: https://yarnpkg.com/lang/en/docs/install/

    You probably just did not install yarn yet.
    Have a look at the yarn installation for Windows here:

    https://yarnpkg.com/lang/en/docs/install/

    See less
    • 6
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
1 2 3 4 … 57

Sidebar

Ask A Question
  • Popular
  • Kenil Vasani

    SyntaxError: invalid syntax to repo init in the AOSP code

    • 5 Answers
  • Kenil Vasani

    xlrd.biffh.XLRDError: Excel xlsx file; not supported

    • 3 Answers
  • Kenil Vasani

    Homebrew fails on MacOS Big Sur

    • 3 Answers
  • Kenil Vasani

    runtimeError: package fails to pass a sanity check for numpy ...

    • 3 Answers
  • Kenil Vasani

    Replicating claims as headers is deprecated and will removed from ...

    • 2 Answers

Explore

  • Most Answered
  • Most Visited
  • Most Voted
  • Random

© 2020-2021 ErrorCorner. All Rights Reserved
by ErrorCorner.com