Home > Enterprise >  JavaScript: Duplicate data property in object literal not allowed in strict mode
JavaScript: Duplicate data property in object literal not allowed in strict mode

Time:07-07

problem Description

I am dealing with an optional variable called ByteRange. For this purpose I added 2 different URLs in $resource.

When I did that I got the following error:

Message:
    Error in parsing: "tools/test/retrieve/retrieve.service.js", Line 24: Duplicate data property in object literal not allowed in strict mode
Details:
    domain: [object Object]
    domainThrown: true

I understood that I can't have 2 gets. Is there another way to deal with optional variables in Javascript?


source code

(function () {
    'use strict';

    angular
        .module('MyApp')
        .factory('retrieve', retrieveObject);

    retrieveObject.$inject = ['$resource'];


    function retrieveObject($resource) {
        this.resource = $resource('api/tools/test', {}, {
            'list': {
                method: 'GET',
                url: 'api/tools/test/list/:chain/:path/:dataCenter'
            },
            'get': {
                method: 'GET',
                url: 'api/tools/test/get/:chain/:dataCenter/:path/:byteRange'
            },
            'get': {
                method: 'GET',
                url: 'api/tools/test/get/:chain/:dataCenter/:path'
            },
        });

        return this;
    }

})();

CodePudding user response:

Okay this is not perfect but it must help you, probably use regex instead of replace is a better idea.

function resolveUrl(url, params) {
    let resolvedUrl = url;

    for(let i in params) {
        resolvedUrl = resolvedUrl.replace(i, params[i]);
    }

    return resolvedUrl;
}

let params = {
    ':chain': 'aaa',
    ':dataCenter': 'bbb',
    ':path': 'ccc',
    ':byteRange': '',
};

let result = resolveUrl('api/tools/test/get/:chain/:dataCenter/:path/:byteRange', params);
console.log(result);
// Output : "api/tools/test/get/aaa/bbb/ccc/"

params = {
    ':chain': 'aaa',
    ':dataCenter': 'bbb',
    ':path': 'ccc',
    ':byteRange': 'yyy',
};

result = resolveUrl('api/tools/test/get/:chain/:dataCenter/:path/:byteRange', params);
console.log(result);
// Output : "api/tools/test/get/aaa/bbb/ccc/yyy"
  • Related