pushing to array on mongodb now works - node.js

I am newby programmer.
I use form that sends data to "/publishdAds". I have used it as router in the server.
When writing this:
router.post("/", function(req, res) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("jobs").update({"email" : req.body.jobLoggedIn}, {$push: {"ads": {req.body.adData}}}, function(err, result) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
});
I am getting this error:
db.collection("jobs").update({"email" : req.body.jobLoggedIn}, {$push: {"ads": {req.body.adData}}}, function(err, result) {
^
SyntaxError: Unexpected token .
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\EYAL\web\fe\angular\jobsFinderProject\server\app.js:14:63)
I have written similar code for inserting, and it works.
What could be the problem?

Remove the curly brackets around
{req.body.adData}
It is syntactically incorrect because curly brackets expect pairs of key: value, or a simple (can not contain dots) name name which is a synonym of name: name.

Related

sqlite3 on node.js 11.1.0 can't open database

I created a database with the newest version of SQLiteStudio. If the database is completely empty, then everything works fine, but already one single table will stop loading the database successfully.
This is the code I use for connecting to the database when the bot starts:
var path = "/home/pi/Desktop/MelloBot";
const dbPath = require(path + '/ItemDatabase.db');
let db = new sqlite3.Database(dbPath, sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.error(err.message);
}
message.channel.send('Connected to the Item database.');
});
And this is the SQLite command for my table:
CREATE TABLE Items (
Name STRING,
ID INTEGER PRIMARY KEY AUTOINCREMENT
);
And in the end, this is the error I get when connecting to a non-empty database:
/home/pi/Desktop/MelloBot/ItemDatabase.db:1
(function (exports, require, module, __filename, __dirname) { SQLite format 3
^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.main (/home/pi/Desktop/MelloBot/adminCommands/item.js:9:24)
I haven't found any solution for this "Format"-error.

Node.js, Express.js - unexpected token {

My app crashes every time it reaches this line:
const {name, price} = req.query;
^
can't seem to locate the exact answer..here is the error log
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:140:18)
at node.js:1043:3
context:
app.get('/products/add' , (req, res) => {
const {name, price} = req.query;
const INSERT_PRODUCTS_QUERY = `INSERT INTO products (name, price) VALUES ('${ name }', ${ price })`;
connection.query(INSERT_PRODUCTS_QUERY, (err,results) => {
if(err)
{
return res.send(err);
}
else
{
return res.send('succesfully added product');
}
});
});
According to node.green, the object destructuring with primitives syntax works after Node.JS v6.4.0, and throws the Unexpected Token { on Node.js versions below that.
Also, the object rest/spread properties only works out of the box from Node v8.6.0. It works in v8.2.1 with the --harmony flag, and throws the Unexpected Token ... on Node.js versions below that.
You tried to use destructuring assignment. AFAIK its support by nodejs v.6+ from a box and from 4.2.2 with flag --harmony_destructuring

How to set a model to a variable in loopback?

fallowing code returns list of models
var models = app.models();
models.forEach(function(Model) {
console.log(Model.modelName);
});
User
AccessToken
ACL
RoleMapping
Role
Registration
Assets
when I using fallowing code to use Registration or Asstes:
D:\apps\newapps\testapp\node_modules\loopback\lib\application.js:129
assert(Model.prototype instanceof Model.registry.getModel('Model'),
^
TypeError: Cannot read property 'prototype' of undefined
at Function.app.model (D:\apps\newapps\testapp\node_modules\loopback\lib\application.js:129:17)
at Object.<anonymous> (D:\apps\newapps\testapp\server\server.js:38:5)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3
I am not expert of loopback but try this code:
var Registration = app.models.Registration;
console.log(Registration);
app.model(Registration);
User is the model name.
var user = app.models.User;
resetPassword is function name of the user model, which
take two parameter new_pwd and token,
user.resetPassword({
new_pwd: new_password,
token
}, (err, data) => {
if (err || !data.status) {
return err || data;
} else {
return data;
}
});

unable to restart my server using node server/index.js

I am trying to run the server using :
node server/index.js
But I am getting this :
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
The index .js file is failing on :
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", (err, data) => {
tables.push(data.name);
});
Any idea what may be wrong ?
Try replacing
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", (err, data) => {
tables.push(data.name);});
with
db.each("SELECT name FROM sqlite_master WHERE type = 'table'", function(err, data) {
tables.push(data.name);});
as it seems your node version is not supporting this syntax.

Pushing Array of Objects Mongoose NodeJS

I have a mongoose schema defined as such,
var userSchema = mongoose.Schema({
token: String,
school: [{name: String, grade: String}]
});
As you can see it is "school" with two values of strings inside it.
I am trying to push in values like this,
app.post('/array', function(req, res) {
user.find({
token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
}, function(err, users) {
if (users.length != 0) {
user.findOne({
token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
}, function(err, doc) {
doc.ar.push("sss", "A");
doc.save();
});
} else {}
});
});
But the push does not work and gives me an error that I cannot get rid of.
doc.ar.push({"sss","A"});
^
SyntaxError: Unexpected token ,
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\..\app.js:27:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
Is there a way to easily push values in?
That's a simple JavaScript syntax error. You probably want this:
doc.ar.push({name: "sss", grade: "A"});

Resources