unable to restart my server using node server/index.js - node.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.

Related

NodeJS mssql error: SyntaxError: Unexpected token '?'

I'm creating a very basic project with Nodejs for accessing SQL Server database. The steps I take are:
I create the folder for the app ang go into it
I execute "npm init -y"
I execute "npm install mssql"
I copy the short example of the documentation (changing the connetion string values) in index.js:
const sql = require('mssql');
async () => {
try {
await sql.connect('Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true');
const result = await sql.query("select * from mytable");
console.dir(result);
} catch (err) {
console.log(err);
}
};
I execute "node index.js"
Then I get the following error:
C:\Users\User\Desktop\project\app\node_modules\tedious\lib\connection.js:1448
const [, major, minor, build] = /^(\d+)\.(\d+)\.(\d+)/.exec(_package.version) ?? ['0.0.0', '0', '0', '0'];
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\Users\User\Desktop\project\app\node_modules\tedious\lib\tedious.js:59:42)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
What can I do for fixing that and be able to execute the code?

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.

pushing to array on mongodb now works

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.

Unable to require node-wit

I had been using node-wit v3.3.2
Today, I wanted to update and use the latest version.
But I'm unable to import node-wit. Not sure why.
I simply copied the code given in their documentation.
'use strict'
var MY_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const {Wit, log} = require('node-wit');
const client = new Wit({
accessToken: MY_TOKEN,
actions: {
send(request, response) {
return new Promise(function(resolve, reject) {
console.log(JSON.stringify(response));
return resolve();
});
},
myAction({sessionId, context, text, entities}) {
console.log(Session ${sessionId} received ${text});
console.log(The current context is ${JSON.stringify(context)});
console.log(Wit extracted ${JSON.stringify(entities)});
return Promise.resolve(context);
}
},
logger: new log.Logger(log.DEBUG) // optional
});
The terminal shows this:
const {Wit, log} = require('node-wit');
^
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:139:18)
at node.js:974:3
Could be the node version you are using. You'll need to use the flag --harmony_destructuring when using a lower version.
Taken from: https://github.com/wit-ai/node-wit
# Node.js <= 6.x.x, add the flag --harmony_destructuring
node --harmony_destructuring examples/basic.js <MY_TOKEN>
# Node.js >= v6.x.x
node examples/basic.js <MY_TOKEN>

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