1

When trying to load the script i get the following error:

name: this.name, online 57

a busy cat

var redis = require('redis');
var db = redis.createClient();

module.exports = Card;
function Card(obj)
{
for(var key in obj)
{
    this[key] = obj[key];
}
}

Card.prototype.save = function(fn)
{
if(this.id)
{
    this.update(fn);
}
else
{
    var card = this;
    db.incr('card:ids', function(err, id)
    {
        if(err) return fn(err);
        card.id = id;
        user.update(fn);
    });
}
};

Card.prototype.update = function(fn)
{
var card = this;
var id      = card.id;
var name    = card.name;
var type    = card.type;
var manaCost = card.manaCost;
var attackPoints = card.att;
var magicPoints = card.magic;
var soulPoints = card.soul;
var cardImage = card.cardImage;
db.set('card:id:' + card.name, id, function(err) 
{
    if (err) return fn(err);
    db.hmset('card:' + id, name, type, manaCost, attackPoints, magicPoints, soulPoints, cardImage, function(err) 
    {
        fn(err);
    });
});
   };

   Card.prototype.toJSON = function()
    {
return 
{
    id: this.id,
    name: this.name,
    type: this.type,
    manaCost: this.manaCost,
    attackPoints: this.attackPoints,
    magicPoints: this.magicPoints,
    soulPoints: this soulPoints,
    cardImage: this.cardImage

}
};

var aquaDragon = new Card({
name: 'Aqua Dragon',
type: 'Rain',
manaCost: '6',
attackPoints: '4',
magicPoints: '5',
soulPoints: '6',
cardImage: '/images/Monsters/Aqua%20Dragon.png'
});

var fireElemental = new Card({
name: 'Fire Elemental',
type: 'Sun',
manaCost: '2',
attackPoints: '0',
magicPoints: '2',
soulPoints: '2'
cardImage: '/images/Monsters/Fire%20Elemental.png'
});

aquaDragon.save(function(err){
if (err) throw err;
console.log('user id %d', aquaDragon.id);
});

fireElemental.save(function(err){
if (err) throw err;
console.log('user id %d', fireElemental.id);
});

The problem is in .toJSON method. When using Name: When using in a other script of mine it is working can't find the problem

Community
  • 1
  • 1
Julien
  • 237
  • 1
  • 3
  • 11

3 Answers3

5

You are dealing with automatic semicolon insertion:

instead of

return // ; a semicolon is automatically inserted here
{

use

return {

read more:

Community
  • 1
  • 1
Ilan Frumer
  • 32,059
  • 8
  • 70
  • 84
1

Don't write return like this

return 
{
    id: this.id,

Instead, write

return {
    id: this.id,

JavaScript finishes the return statement by inserting a semi-colon.

Foobie Bletch
  • 214
  • 1
  • 9
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

{ should be on the same line as the return statement, because JS interpreter inserts one after a return statement on the same line.

I strongly suggest to stop using semicolons completely and learn how ASI works, because without that you'd be hitting these errors once in a while...

alex
  • 11,935
  • 3
  • 30
  • 42