SyntaxError: Unexpected token * after importing sequelize model db object - javascript

I am working on an app where I have to connect and perform queries on an SQL server using Sequelize. I have created migrations, seeders, and models using sequelize init but now when I tried to create an object of models using const db = require("./models") it is throwing error like
D:\Code Practice\express-sequelize-demo\node_modules\tedious\lib\token\stream-parser.js:85
static async *parseTokens(iterable, debug, options, colMetadata = []){
SyntaxError: Unexpected token *
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.<anonymous> (D:\Code Practice\express-sequelize-demo\node_modules\tedious\lib\token\token-stream-parser.js:10:44)
Files I created
package.json
{
"name": "express-sequelize-demo",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mssql": "^7.1.0",
"sequelize": "^6.6.2",
"tedious": "^11.0.9"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
app.js
const express = require("express");
const app = express();
const PORT = 8088;
// throwing error here after importing
const db = require("./models");
app.get("/users", (req, res) => {
res.send({
status: 1,
message: "Hello from User",
});
});
app.get("/", (req, res) => {
res.send({
status: 1,
message: "Welcome to Home Page",
});
});
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
models/index.js
"use strict";
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
console.log(sequelize);
}
fs.readdirSync(__dirname)
.filter((file) => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach((file) => {
const model = require(path.join(__dirname, file))(
sequelize,
Sequelize.DataTypes
);
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
models/user.js
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
// define association here
}
};
User.init({
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
bio: DataTypes.TEXT
}, {
sequelize,
modelName: 'User',
});
return User;
};
5)config.json
{
"development": {
"username": "SA",
"password": "Password123",
"database": "database_development",
"host": "localhost",
"dialect": "mssql"
},
"test": {
"username": "root",
"password": "Password123",
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mssql"
},
"production": {
"username": "root",
"password": "Password123",
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mssql"
}
}

I have resolved this issue by updating the node version.
Here the issue was when I try to create sequelize instances using
const sequelize = new Sequelize(DB connection params); //pass the actual db config
it was throwing that error.
SyntaxError: Unexpected token *
But after some research, I updated my node version from 8.11.4 to 14.17.0 and it worked. Now without any error, I can run my application and it is working as expected.

Related

OpenAI API in node gives basic Await error. How do I fix?

I literally copied the code from the openAI example and it gives me a remedial Await JS error but I am unsure what it expects me to do. I just want to spin up an Express.js instance and get a hello world from openapi (eventually chatgpt). The web server works fine.
Here is my doinker:
const express = require('express')
const app = express()
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "my key is here"
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: "Hello world",
});
console.log(completion.data.choices[0].text);
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
Error:
SyntaxError: await is only valid in async functions and the top level bodies of modules
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1088:15)
at Module._compile (node:internal/modules/cjs/loader:1123:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
The original OpenAI example code is the same. Why the hell is this the example code?
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: "Hello world",
});
console.log(completion.data.choices[0].text);
As already mentioned, you need to wrap your async code in a function:
const express = require('express')
const app = express()
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "my key is here"
});
const openai = new OpenAIApi(configuration);
const completionFunction = async () => {
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: "Hello world",
});
console.log(completion.data.choices[0].text);
};
completionFunction();
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
Also, make sure you have a package.json file with the appropriate dependencies:
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"openai": "^3.1.0"
}
}
you can't use await without async function
create new async function:
async doStuff() {
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: "Hello world",
});
return completion;
}
doStuff();
the documentation probably uses deno, a new version of nodejs where you can use await outside of async function
and why didn't you use ChatGPT to fix the error? :)

Why Sequelize migration create table but models can not connect to a database

I am learning how to use Sequelize ORM in Nodejs and save data in Postgres Database.
My goal is to insert user data into Users table. I have created the table using migration, and it works. However, I am not able to save users data. I 've followed many resources for example Tut 1 Tut 2, etc.. , I still get the same error
C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\database\models\index.js:12
if (config.use_env_variable) {
^
TypeError: Cannot read property 'use_env_variable' of undefined
at Object.<anonymous> (C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\database\models\index.js:12:12)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at babelWatchLoader (C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\node_modules\babel-watch\runner.js:51:13)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\node_modules\babel-watch\runner.js:62:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\HP\Desktop\Andela\project\Tutorials\react-project\chat_app_api\server\server.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at babelWatchLoader (C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\node_modules\babel-watch\runner.js:51:13)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\HP\Desktop\path\project\Tutorials\react-project\chat_app_api\node_modules\babel-watch\runner.js:62:7)
config/config.js
require('dotenv').config();
module.exports = {
development: {
use_env_variable: 'DATABASE_URL_DEV',
dialect: 'postgres',
},
test: {
use_env_variable: 'DATABASE_URL_TEST',
dialect: 'postgres',
},
production: {
use_env_variable: 'DATABASE_URL',
dialect: 'postgres',
ssl: true,
dialectOptions: {
ssl: true,
},
},
};
migrations/20190927083519-create-user.js
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
fullname: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
username: {
type: Sequelize.STRING
},
telephone: {
type: Sequelize.STRING
},
image: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
models/index.js
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env]; // why this return Undefined ?
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
models/users
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
fullname: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
username: DataTypes.STRING,
telephone: DataTypes.STRING,
image: DataTypes.STRING
}, {});
User.associate = function (models) {
// associations can be defined here
};
return User;
};
app.js
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import { errors } from 'celebrate';
import routes from './Routes/index';
const app = express();
app.use(cors());
app.use(morgan('combined'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', routes);
app.use(errors());
app.use((req, res) => {
const error = new Error('Route not found');
error.status = 404;
return res.status(error.status).json({
status: error.status,
message: error.message,
});
});
// Server Error
app.use((error, req, res) => {
const status = error.status || 500;
return res.status(status).json({
status,
message: error.message || 'Server error',
});
});
export default app;
.env
DATABASE_URL_DEV=postgres://postgres:.#localhost:5432/db_dev
DATABASE_URL_TEST=postgres://postgres:.#localhost:5432/db_test
DATABASE_URL=postgres://user:password#host:5432/db_remote
controllers/userControllers.js
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import models from '../../database/models';
import uploadImage from '../Helpers/upload.Image';
dotenv.config();
class UserController {
static async signup(req, res) {
const { body: input } = req;
input.password = bcrypt.hashSync(input.password, 10);
try {
const image = await uploadImage(req, res);
const { secure_url: img } = await image;
input.image = img;
console.log('result before ########################', models.User); // Undefined
const result = await models.User.create(input);
console.log('result after ########################', result); // Error here
delete result.dataValues.password;
const token = jwt.sign(result.dataValues, process.env.SECRET_KEY, { expiresIn: '1W' });
result.dataValues.token = token;
const status = 201;
return res.status(status).json({
status,
message: 'User successfully created',
data: result.dataValues,
});
} catch (error) {
console.log('error########################', error);
let { message } = error.errors[0];
const status = 500;
message = message || 'Server error';
return res.status(status).json({
status,
message,
});
}
}
}
export default UserController;
I still don't know why in models/index.js my config variable return undefined.
require(__dirname + '/../config/config.js') // return object
env // return environment
const config = require(__dirname + '/../config/config.js')[env]; //return Undefined
I spent 3 days debugging but I can not solve the Error. any help, guidance is highly appreciated.
Thanks
Guyz, I found an answer to my problem,
in models/index.js
I change process.env.NODE_ENV to process.env.NODE_ENV.trim()
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
// Before
const env = process.env.NODE_ENV || 'development';
// After
const env = process.env.NODE_ENV.trim() || 'development'; // add .trim()
const config = require(__dirname + '/../config/config.js')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
...
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Further Details
package.json
"scripts": {
"db:migrate:dev": "sequelize db:migrate --env development",
"db:migrate:test": "sequelize db:migrate --env test",
"db:migrate:production": "sequelize db:migrate --env production",
"db:reset": "sequelize db:migrate:undo",
"start": "SET NODE_ENV=production && babel-watch server/server.js",
"dev": "SET NODE_ENV=development && babel-watch server/server.js",
"test": "SET NODE_ENV=testing && babel-watch server/server.js"
}
Example, Let's say if I start the server by typing in the terminal
npm run dev
If i do console.log(process.env.NODE_ENV) // output is "development " with a space.
Hence,
process.env.NODE_ENV === "development" // return false
or
"development " === "development" // return false
Javascript Trim() remove whitespace from both sides of a string
You want more resource? please visit w3c

ERROR: Error parsing url: undefined when migrating database to heroku

I'm using Sequelize as an ORM and I'm trying to migrate my database to Heroku. When running heroku run sequelize db:migrate
I just get
Loaded configuration file "config/config.js".
Using environment "production".
ERROR: Error parsing url: undefined
Here's what my config file looks like:
module.exports = {
"development": {
"username": "root",
"password": "password",
"database": "vueapp",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"use_env_variable": process.env.DATABASE_URL,
"dialect": "postgres",
"ssl": true,
"dialectOptions": {
"ssl": true
}
}
}
And the index file
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config')[env];
const User = require("./user")
const Hour = require('./hours');
const db = {
User,
Hour
};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(config.use_env_variable, config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Not entirely sure what the issue is. Going through the heroku logs, I see a throw new Error('Dialect needs to be explicitly supplied as of v4.0.0');
But I have added the dialect in the config.js file. Does it matter if the config file is a .js file or a .json file?
Just to clarify, the actual web app opens up and I can see the error handler that I set up. So I'm not actually getting an application error when opening up the route URL
production: {
use_env_variable: 'DATABASE_URL',
},
change your production to this and go to heroku to set environmental variable
Did you later solve the problem
This is a little confusing but Damilola is correctly. You need need to leave the value for use_env_variable to 'DATABASE_URL'. You will be tempted to change this to process.env.DATABASE_URL. This is incorrect.
Leaving it like this will allow Sequelize to inject the value that is saved to the Heroku env variable DATABASE_URL.
production: {
use_env_variable: 'DATABASE_URL',
dialect: "postgres",
protocol: "postgres"
}

Sequelize sqlite3 connects to database but throw error when fetching data

I'm using sequelize with sqlite3 inside nodejs.
The connection to database file runs fine with no error, but when trying to access it for the first time I'm getting the following error (no matter what table or query I try to do):
TypeError: Cannot read property 'findOne' of undefined
at l.a.sequelize.authenticate.then (/usr/local/bin/aeirtu/node/webpack:/server.js:55:1)
at tryCatcher (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:763:18)
at tryOnImmediate (timers.js:734:5)
at processImmediate (timers.js:716:5)
Here is my connection code:
My node.js code:
import express from "express";
import models from "./models";
const app = express();
models.sequelize
.authenticate()
.then(() => {
console.log("Connected to repository");
})
.catch(err => {
console.log("ERROR connecting to repository.");
console.log(err);
});
let port = 3001;
app.listen(port, () => {
console.log("Server listening on port " + port);
});
My models.js:
import Sequelize from "sequelize";
import fs from "fs";
import path from "path";
const DATABASE_FILENAME = "test.db";
const fileWithPath = path.join(process.env.TEST_HOME, 'data', DATABASE_FILENAME);
let basename = path.basename(__filename);
let env = process.env.NODE_ENV || "development";
let db = {};
console.log("Connecting to repository at " + fileWithPath);
// database wide options
let opts = {
dialect: "sqlite",
storage: fileWithPath,
define: {
//prevent sequelize from pluralizing table names
freezeTableName: true,
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false
},
operatorsAliases: Sequelize.Op, // use Sequelize.Op https://github.com/sequelize/sequelize/issues/8417#issuecomment-334056048
};
let sequelize = new Sequelize(DATABASE_FILENAME, null, null, opts);
fs
.readdirSync(__dirname)
.filter(file => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach(file => {
let model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
export default db;
And a simple test code:
import db from "./models";
db.TestTable.findOne().then(data => {
if (!data) console.log("ERROR FETCHING DATA");
console.log(data.name);
});
I've triple checked: the sqlite3 file is in the correct location and the tables are there with data. For some reason I'm not being able to access then. I've also changed chmod a+rwx at the database file.
you forgot to import the *TestTable model`
db.TestTable = require('./TestTable')(sequelize, Sequelize);
your model.js should be a connection
but there are no define model yet like
module.exports = (sequelize, DataTypes) => {
const testTable = sequelize.define('model', {
states: {
type: Sequelize.ENUM,
values: ['active', 'pending', 'deleted']
}
});
return testTable;
};
Sample

Syntax Error: Unexpected Token async()

I have been following this tutorial on using GraphQL and it has told me to write this block of code in my src/index.js file:
const express = require('express');
const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');
const schema = require('./schema');
// 1
const connectMongo = require('./mongo-connector');
// 2
const start = async () => {
// 3
const mongo = await connectMongo();
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
};
// 5
start();
Though when I try to run the code using: node ./src/index.js it gives me this error:
const start = async () => {
^
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.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
I've searched online and it seems that if might be caused by the node.js version from here but I've checked my noed.js version and it's 8.3.0 so it should be supported without having to use Babel if I'm not mistaken.
Here is my package.json file:
{
"name": "graphql-js-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^1.1.2",
"body-parser": "^1.17.2",
"express": "^4.15.4",
"graphql": "^0.11.2",
"graphql-tools": "^1.2.2",
"mongodb": "^2.2.31"
}
}
async functions are only available since node 8.3
your code is equivalent to (without async/await)
const start = () => {
return connectMongo().then(mongo => {
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
return;
});
};
start your app by the command node yourAppFile -harmony!
async function is available in the harmony mode under Node7.+

Categories

Resources