sqlite3 on node.js 11.1.0 can't open database - node.js

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.

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?

Azure Chatbot fails with "SyntaxError: Unexpected identifier - cosmosClient.databases.createIfNotExists" while connecting to CosmosDB

I am extremely new to Azure Bot Services and the Azure platform as a whole.
I am trying to create a Chatbot using node.js but I am getting the below error while trying to connect to CosmosDB.
The bot was running fine before I added the below code to connect to CosmosDB.
Any help or guidance on this would be appreciated!
P.S. - I have added the '#azure/cosmos' package and the code runs without any error if I just remove the try-catch segment.
Code for connecting to CosmosDB:
var async=require("async");
var await=require("await");
const CosmosClientInterface = require("#azure/cosmos").CosmosClient;
const databaseId = "ToDoList";
const containerId = "custInfo";
const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";
const cosmosClient = new CosmosClientInterface({
endpoint: endpoint,
auth: {
masterKey: authKey
},
consistencyLevel: "Session"
});
async function readDatabase() {
const { body: databaseDefinition } = await cosmosClient.database(databaseId).read();
console.log(`Reading database:\n${databaseDefinition.id}\n`);
}
Error Message:
Sat Jan 12 2019 03:40:08 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
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> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:570:32)
Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
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> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:570:32)
You can't await without being in an async function.
Dump all your code into a async function main(){} method, then call main().catch((err) => console.log(err)); or some similar thing to start the promise and handle errors.
You can see a sample of that kind of pattern here in this sample: https://github.com/Azure/azure-cosmos-js/blob/master/samples/ChangeFeed/app.js#L33
--- EDIT 1 ---
Here's your sample rewritten with Promises:
const CosmosClientInterface = require("#azure/cosmos").CosmosClient;
const databaseId = "ToDoList";
const containerId = "custInfo";
const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";
const cosmosClient = new CosmosClientInterface({
endpoint: endpoint,
auth: {
masterKey: authKey
},
consistencyLevel: "Session"
});
cosmosClient.database(databaseId).read().then(({body: databaseDefinition}) => {
console.log(`Reading database:\n${databaseDefinition.id}\n`);
}).catch((err) {
console.err("Something went wrong" + err);
});
For your sample above, you don't need to import async/await, they are keywords in JavaScript now.
Here's a blog post that compares and contrasts Async/Await and Promises: https://hackernoon.com/should-i-use-promises-or-async-await-126ab5c98789

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.

SyntaxError in plugin 'gulp-mocha' Unexpected token =

I am getting the following error in Travis-ci when using Node 4.3.2:
SyntaxError in plugin 'gulp-mocha' Unexpected token =
Everything works when using Node 6, but not 4.3.2. Here is the stack trace:
[12:05:11] Starting 'build'...
[12:05:11] Finished 'build' after 13 μs
[12:05:11] Starting 'tests'...
SyntaxError in plugin 'gulp-mocha'
Message:
Unexpected token =
Stack:
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
This is the task I am running:
gulp.task('tests', ['build'], (cb) => {
if (hasError) {
cb();
return;
}
return gulp.src('build/test/**/*.js', {read: false})
.pipe(mocha())
.on("error", handleError);
});
How can I correct this?
The older version of Node is being used, as this is an AWS Lambda application.

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