MisterniceGuy
MisterniceGuy

Reputation: 1796

Why am I getting the error "Identifier ... has already been declared"?

I am having an issue calling a function which returns an object. The function looks like this.

function upsertDoc(bucketName,docID,doc){
        let bucket = bucketWithName(bucketName)
        return new Promise((resolve,reject)=>{
            bucket.upsert(docID,doc,(err, result2)=>{
            if(err) return reject(err);
            return resolve({docID, result});
            });
        });
        }

it it works fine if i call it like this

let { docID, result } = await couch.upsertDoc('contacts',id, myLookup)

the problem i have is that if i call prior to this call

let { docID , result} = await couch.getDoc('contacts',id)

the system complains "SyntaxError: Identifier 'result' has already been declared "
So the question is how can i reassign docID and result to my function as

{ docID, result } = await couch.upsertDoc('contacts',id, myLookup)

does not work

Upvotes: 2

Views: 1795

Answers (1)

Cal Irvine
Cal Irvine

Reputation: 1144

You need to change the name of one of your results.

let { docID: docId2 , result: result2} =  await couch.upsertDoc('contacts',id, myLookup)

This, for instance, will rename the result from this instance to result2.

You can't declare the same variable name in the same scope.

If you want to reassign them to the same variable, you'll have to do that after the fact.

let { docID: docID2 , result: result2} =  await couch.upsertDoc('contacts',id, myLookup)
docID = docID2;
result = result2;

Upvotes: 2

Related Questions