Categories
discuss

Node.js – “TypeError – res.setHeader is not a function”

I’m trying to load JSON from a URL to a variable and send it back to the client’s javascript

var getJSON =require('get-json');

app.post('/json', function(req, res) {
    getJSON(url, function(err, res){
        if(err)
        {
           console.log(err);
        }
        else
        {
           res.setHeader('content-type', 'application/json');
           res.send(JSON.stringify({json: res.result}));
        }
    });
});

Every time I run the code the server says that res.setHeader isn’t a function and the rest breaks.

Answer

Both post and getJSON callbacks have same res variable name. Try this:

var getJSON =require('get-json');

app.post('/json', function(req, res) {
  getJSON(url, function(err, response){
    if(err)
    {
       console.log(err);
    }
    else
    {
       res.setHeader('content-type', 'application/json');
       res.send(JSON.stringify({json: response.result}));
    }
  });
});
Categories
discuss

TransformException duplicate entry for common.annotations.Beta

This started when I added google-api-services-calendar. I am getting this error when trying to build:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
     com.android.build.transform.api.TransformException: java.util.zip.ZipException: 
     duplicate entry: com/google/common/annotations/Beta.class

This is part of the output when running ./gradlew app:dependencies:

compile - Classpath for compiling the main sources.
+--- com.google.android.gms:play-services-measurement:8.1.0
|    +--- com.google.android.gms:play-services-base:8.1.0
|    |    --- com.google.android.gms:play-services-basement:8.1.0
|    |         --- com.android.support:support-v4:22.2.0 -> 23.0.1
|    |              --- com.android.support:support-annotations:23.0.1
|    --- com.google.android.gms:play-services-analytics:8.1.0
|         +--- com.google.android.gms:play-services-basement:8.1.0 (*)
|         --- com.google.android.gms:play-services-ads:8.1.0
|              +--- com.google.android.gms:play-services-basement:8.1.0 (*)
|              --- com.google.android.gms:play-services-appindexing:8.1.0
|                   --- com.google.android.gms:play-services-basement:8.1.0 (*)
+--- com.google.dagger:dagger:2.0
|    --- javax.inject:javax.inject:1
+--- com.google.android.gms:play-services-identity:8.1.0
|    --- com.google.android.gms:play-services-base:8.1.0 (*)
+--- com.google.android.gms:play-services-plus:8.1.0
|    --- com.google.android.gms:play-services-base:8.1.0 (*)
+--- com.google.android.gms:play-services:8.1.0
|    +--- com.google.android.gms:play-services-ads:8.1.0 (*)
|    +--- com.google.android.gms:play-services-analytics:8.1.0 (*)
|    +--- com.google.android.gms:play-services-appindexing:8.1.0 (*)
|    +--- com.google.android.gms:play-services-appinvite:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-appstate:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-basement:8.1.0 (*)
|    +--- com.google.android.gms:play-services-cast:8.1.0
|    |    +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    |    --- com.android.support:mediarouter-v7:22.2.0
|    |         --- com.android.support:appcompat-v7:22.2.0 -> 23.0.1
|    |              --- com.android.support:support-v4:23.0.1 (*)
|    +--- com.google.android.gms:play-services-drive:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-fitness:8.1.0
|    |    +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    |    --- com.google.android.gms:play-services-location:8.1.0
|    |         +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    |         --- com.google.android.gms:play-services-maps:8.1.0
|    |              --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-games:8.1.0
|    |    +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    |    --- com.google.android.gms:play-services-drive:8.1.0 (*)
|    +--- com.google.android.gms:play-services-gcm:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-identity:8.1.0 (*)
|    +--- com.google.android.gms:play-services-location:8.1.0 (*)
|    +--- com.google.android.gms:play-services-maps:8.1.0 (*)
|    +--- com.google.android.gms:play-services-measurement:8.1.0 (*)
|    +--- com.google.android.gms:play-services-nearby:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-panorama:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-plus:8.1.0 (*)
|    +--- com.google.android.gms:play-services-safetynet:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-vision:8.1.0
|    |    --- com.google.android.gms:play-services-base:8.1.0 (*)
|    +--- com.google.android.gms:play-services-wallet:8.1.0
|    |    +--- com.google.android.gms:play-services-base:8.1.0 (*)
|    |    +--- com.google.android.gms:play-services-identity:8.1.0 (*)
|    |    --- com.google.android.gms:play-services-maps:8.1.0 (*)
|    --- com.google.android.gms:play-services-wearable:8.1.0
|         --- com.google.android.gms:play-services-base:8.1.0 (*)
+--- com.google.guava:guava:18.0
+--- com.google.maps.android:android-maps-utils:0.4
+--- com.google.apis:google-api-services-calendar:v3-rev125-1.20.0
|    --- com.google.api-client:google-api-client:1.20.0
|         +--- com.google.oauth-client:google-oauth-client:1.20.0
|         |    +--- com.google.http-client:google-http-client:1.20.0
|         |    |    --- com.google.code.findbugs:jsr305:1.3.9
|         |    --- com.google.code.findbugs:jsr305:1.3.9
|         +--- com.google.http-client:google-http-client-jackson2:1.20.0
|         |    +--- com.google.http-client:google-http-client:1.20.0 (*)
|         |    --- com.fasterxml.jackson.core:jackson-core:2.1.3 -> 2.2.2
|         --- com.google.guava:guava-jdk5:13.0
+--- com.android.support:design:23.0.1
|    +--- com.android.support:support-v4:23.0.1 (*)
|    --- com.android.support:appcompat-v7:23.0.1 (*)
+--- com.android.support:recyclerview-v7:23.0.1
|    +--- com.android.support:support-v4:23.0.1 (*)
|    --- com.android.support:support-annotations:23.0.1
+--- com.android.support:cardview-v7:23.0.1
+--- com.android.support:palette-v7:23.0.1
|    --- com.android.support:support-v4:23.0.1 (*)
+--- com.android.support:appcompat-v7:22.2.0 -> 23.0.1 (*)
+--- com.android.support:support-v4:22.2.0 -> 23.0.1 (*)
+--- com.firebase:firebase-client-android:2.3.1
|    --- com.firebase:firebase-client-jvm:2.3.1
|         +--- com.fasterxml.jackson.core:jackson-databind:2.2.2
|         |    +--- com.fasterxml.jackson.core:jackson-annotations:2.2.2
|         |    --- com.fasterxml.jackson.core:jackson-core:2.2.2
|         --- com.firebase:tubesock:0.0.11
+--- com.firebase:firebase-client-jvm:2.3.0 -> 2.3.1 (*)
+--- net.danlew:android.joda:2.8.0
|    --- joda-time:joda-time:2.8
+--- org.joda:joda-convert:1.2
+--- com.squareup:otto:1.3.5
+--- com.jakewharton:butterknife:7.0.1
+--- org.apache.commons:commons-lang3:3.4
+--- org.apache.commons:commons-collections4:4.0
--- com.bignerdranch.android:expandablerecyclerview:1.0.3
     --- com.android.support:recyclerview-v7:22.2.0 -> 23.0.1 (*)

I have tried adding this to all my dependencies:

compile (some.dependencie.that.may.or.may.not.have.the.duplicate){
    exclude group: 'com.google.common', module: 'annotations'
}

I tried adding this to my android block:

configurations {
   all*.exclude group: 'com.google.common', module: 'annotations'
}    

with these variations:

 all*.exclude module: 'annotations`
 all*.exclude group: 'com.google.common'
 all*.exclude group: 'com.google.common.annotations'

This is the build.gradle before any attempts to fix error:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Google
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-plus:8.1.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.google.guava:guava:18.0'
    compile 'com.google.maps.android:android-maps-utils:0.4'
    compile('com.google.apis:google-api-services-calendar:v3-rev125-1.20.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }

    // Android Design
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:palette-v7:23.0.1'

    // Android Support
    compile('com.android.support:appcompat-v7:22.2.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    compile('com.android.support:support-v4:22.2.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }

    // Firebase
    compile('com.firebase:firebase-client-android:2.3.1') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    compile('com.firebase:firebase-client-jvm:2.3.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }

    // Joda Time
    compile 'net.danlew:android.joda:2.8.0'
    compile 'org.joda:joda-convert:1.2'

    // Square
    compile 'com.squareup:otto:1.3.5'
    compile 'com.jakewharton:butterknife:7.0.1'
    provided 'javax.annotation:jsr250-api:1.0'

    // Common
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'org.apache.commons:commons-collections4:4.0'

    compile 'com.bignerdranch.android:expandablerecyclerview:1.0.3'
}

I have searched my project for Beta.class and found that it is only being used in some jars; com.google.common.annotations, com.google.api.client.util and dagger.internal. I tried to add the exclude group to those related dependencies and still see the same error.

I have been stuck here for way too long. Any suggestions?

Answer

Exclude group: 'com.google.guava' from play services related dependencies.

For example:

compile ('com.google.android.gms:play-services:8.1.0'){
    exclude group: 'com.google.guava'
}

P.S. Before getting your error I’ve faced with a lot of different, so my final dependencies list is:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Google
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
    compile ('com.google.android.gms:play-services-identity:8.1.0'){
        exclude group: 'com.google.guava'
    }
    compile ('com.google.android.gms:play-services-plus:8.1.0'){
        exclude group: 'com.google.guava'
    }
    compile ('com.google.android.gms:play-services:8.1.0'){
        exclude group: 'com.google.guava'
    }
    compile 'com.google.guava:guava:18.0'
    compile 'com.google.maps.android:android-maps-utils:0.4'

    compile('com.google.apis:google-api-services-calendar:v3-rev125-1.20.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'com.android.support'
        exclude module: 'support-annotations'
        exclude group: 'com.google.guava'
    }

    // Android Design
    compile ('com.android.support:design:23.0.1'){
        exclude group: 'com.android.support'
    }
    compile ('com.android.support:recyclerview-v7:23.0.1') {
        exclude group: 'com.android.support'
    }

    compile 'com.android.support:cardview-v7:23.0.1'
    compile ('com.android.support:palette-v7:23.0.1'){
        exclude group: 'com.android.support'
    }

    // Android Support
    compile('com.android.support:appcompat-v7:22.2.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
        exclude group: 'com.android.support'
    }

    compile 'com.android.support:support-annotations:23.0.1'
    compile('com.android.support:support-v4:22.2.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
        exclude module: 'support-annotations'
    }

    // Firebase
    compile('com.firebase:firebase-client-android:2.3.1') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    compile('com.firebase:firebase-client-jvm:2.3.0') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }

    // Joda Time
    compile 'net.danlew:android.joda:2.8.0'
    compile 'org.joda:joda-convert:1.2'

    // Square
    compile 'com.squareup:otto:1.3.5'
    compile 'com.jakewharton:butterknife:7.0.1'
    provided 'javax.annotation:jsr250-api:1.0'

    // Common
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'org.apache.commons:commons-collections4:4.0'

    compile ('com.bignerdranch.android:expandablerecyclerview:1.0.3'){
        exclude group: 'com.android.support'
    }
}
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..