Reputation: 655
I'm trying to insert a static array to my firebase db. This is what my current db looks like.
What I wanted to create is something like this.
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
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.
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).
Upvotes: 2
Reputation: 1256
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:
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.
Many programmers prefer to use underscores (date_of_birth), especially in SQL databases.
Underscores are often used in PHP documentation.
PascalCase is often preferred by C programmers.
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