1

I'm trying to remove an object from the cart using its art_id, but when I'm getting this error SyntaxError: Delete of an unqualified identifier in strict mode. Why is this happening and how can I modify my code to come over this error

 function remove_from_cart(req, res, next) {
        console.log(req.session);
        var art_id = req.params._id;

        var art_to_remove = _.findWhere(req.session.cart, {
            art_id: art_id
        });

        console.log(art_to_remove);
        delete art_to_remove;
        console.log(req.session);
        res.send('deleted')
    }
Ankit Kumar
  • 193
  • 2
  • 10
  • What do the first and second `console.log()` show? It sounds like it's not finding the object you want to delete. – Reinstate Monica Cellio May 09 '16 at 10:02
  • 1
    please refer http://stackoverflow.com/questions/10643587/motive-behind-strict-mode-syntax-error-when-deleting-an-unqualified-identifier – Subburaj May 09 '16 at 10:02
  • Possible duplicate of [Why is delete not allowed in Javascript5 strict mode?](http://stackoverflow.com/questions/16652589/why-is-delete-not-allowed-in-javascript5-strict-mode) – A G May 09 '16 at 10:03
  • @Archer req.session contains an array of cart, the cart is supposed to have some objects. – Ankit Kumar May 09 '16 at 10:06

6 Answers6

5

Since it seems you're using underscore, you can use _.reject():

req.session.cart = _.reject(req.session.cart, { art_id: art_id });
robertklep
  • 198,204
  • 35
  • 394
  • 381
1

I'm getting this error SyntaxError: Delete of an unqualified identifier in strict mode.

As per spec

When a delete operator occurs within strict mode code, a SyntaxError exception is thrown if its UnaryExpression is a direct reference to a variable, function argument, or function name

You need to use splice to delete values in array rather than deleting a variable (not allowed) which are pointing to filtered values.

Or using underscore, you can get the index of the fetched value using _findWhere as well

var b = _.indexOf(req.session.cart, art_to_remove );
req.session.cart.splice(b,1);

A simple JS implemention of the same could be

   req.session.cart = req.session.cart.filter(function(cart){
     return cart.art_id != art_id;
   })
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Here `cart` is an array and deleting item from an array should be possible right(although not the correct way)? `art_to_remove` is neither *direct reference to a variable, function argument, or function name* – sabithpocker May 09 '16 at 10:18
  • @sabithpocker `art_to_remove` is a variable. – gurvinder372 May 09 '16 at 10:20
  • But that is a reference to an object which is an array element, which qualifies to be deleted in strict mode. See [this discussion](http://stackoverflow.com/a/12025291/427146) on why this is made as a `Syntax Error` instead of a `Type Error`. – sabithpocker May 09 '16 at 10:53
  • @sabithpocker It is still a variable. Variables don't get deleted. Apart from reference value, they have reference name as well. Simply try this `var a = {}; delete a;` it will output `false` since it is not an allowed operation. – gurvinder372 May 09 '16 at 10:58
0

Instead of using Delete you have to use Array.prototype.splice so your code should look like this:

 function remove_from_cart(req, res, next) {
    console.log(req.session);
    var art_id = req.params._id;

    var art_to_remove = _.findWhere(req.session.cart, {
        art_id: art_id
    });

    console.log(art_to_remove);
    req.session.cart.splice(req.session.cart.indexOf(art_to_remove), 1);
    console.log(req.session);
    res.send('deleted')
}
Nick Messing
  • 494
  • 5
  • 14
0

The following example works:

(function() {
  'use strict';
  var foo = [{x:1}, {y:2}, {z:3}];
  alert(delete foo[0]);
}());

While the below one doesn't:

(function() {
  'use strict';
  var foo = [{x:1}, {y:2}, {z:3}];
  var y = foo[0];
  delete y;
}());

So for delete in strict mode you should probably use a syntax like array[index] or object[property] or object.property. A variable referring to the same doesn't work.

Also, delete on an array is not recommended as it doesn't update the length of the array and leads to holes in array. Its always better to use splice.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

i am expecting that req.session.cart is an array object which is not empty and req.params._id is not empty

function remove_from_cart(req, res, next) {
    var cart = req.session.cart;
    var art_id = req.params._id;

    var pos = cart.map(function(x) {return x.id; }).indexOf(art_id);
    var art_to_remove = cart[pos];
    cart.splice(pos, 1);

    console.log(art_to_remove);
    console.log(cart);

    res.send('deleted')
}
Manoj Ojha
  • 373
  • 2
  • 11
0

We can't directly delete object from an array.Have a look at this answer..
How do I remove an object from an array with JavaScript?

Community
  • 1
  • 1