Lion Smith
Lion Smith

Reputation: 655

Invalid or unexpected token using post method to firebase database

I'm trying to insert a static array to my firebase db. This is what my current db looks like. enter image description here

What I wanted to create is something like this. enter image description here

router.post('/:uuid', function (req, res) {
    var ref = database.ref('watchers/' + req.params.uuid);
    var body = _.pick(_.assign(addwatcher, req.body), _.keys(addwatcher)); 

    body['createdAt'] = moment().format('YYYY-MM-DD HH:mm:ss');

    body['totalRating'] = {1star:0,2star:0,3star:0,4star:0,5star:0,total:0};

    ref.set(body);

    res.send('Sucessfully authenticated!');

});

I'm using this script to save my auth id instead of pushId, but when I tried to but my problem when I tried to create an array it sends error on my console..

body['totalRating'] = {1star:0,2star:0,3star:0,4star:0,5star:0,total:0};
                       ^
SyntaxError: Invalid or unexpected token

I just wanted to know on how to insert array data on .post method.

Upvotes: 1

Views: 405

Answers (2)

jofftiquez
jofftiquez

Reputation: 7708

You can't use number as first character in naming a variable in javascript. Try it in your developer's console you'll get this.

enter image description here

Update

If you really want to achieve that you can do this {'1star': 0}. Just wrap the var name between single quotation marks (or double quotation marks).

enter image description here

Upvotes: 2

Harun Diluka Heshan
Harun Diluka Heshan

Reputation: 1256

Naming Conventions

All names start with a letter.

Always use the same naming convention for all your code. For example:

Variable and function names written as camelCase

Global variables written in UPPERCASE (We don't, but it's quite common)

Constants (like PI) written in UPPERCASE

Should you use hyp-hens, camelCase, or under_scores in variable names?

This is a question programmers often discuss. The answer depends on who you ask:

Hyphens in HTML and CSS:

HTML5 attributes can start with data- (data-quantity, data-price).

CSS uses hyphens in property-names (font-size).

Hyphens can be mistaken as subtraction attempts. Hyphens are not allowed in JavaScript names.

Underscores:

Many programmers prefer to use underscores (date_of_birth), especially in SQL databases.

Underscores are often used in PHP documentation.

PascalCase:

PascalCase is often preferred by C programmers.

camelCase:

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries.

Do not start names with a $ sign. It will put you in conflict with many JavaScript library names.

Naming Conventions For Javascript

Upvotes: 2

Related Questions