sequelize.js: Unhandled rejection SequelizeForeignKeyConstraintError - node.js

I am trying to insert/update data into a postgresql db using the following function:
var updateStudentDataAndReturn = function(req, res, { redirectLink: redirectLink, studentId: studentId }) {
models.Student.update({
firstName: req.body.firstName,
lastName: req.body.lastName,
phoneNumber: req.body.phoneNumber,
skypeUsername: req.body.skypeUsername,
typeOfStudent: req.body.typeOfStudent,
school: req.body.school,
expectedYearOfGraduation: req.body.expectedYearOfGraduation,
TeamId: req.body.TeamId
}, {
where: {
id: studentId
}
}).then(() => {
return models.StudentMajor.destroy({
where: {
StudentId: studentId
}
})
}).then(() => {
return models.StudentSchool.destroy({
where: {
StudentId: studentId
}
})
}).then(() => {
if (!Array.isArray(req.body.major)) {req.body.major = [req.body.major]};
return req.body.major.forEach((major) => {
return models.StudentMajor.create({
StudentId: studentId,
major: major
});
});
}).then(() => {
if (!Array.isArray(req.body.school)) {req.body.school = [req.body.school]};
return req.body.school.forEach((school) => {
return models.StudentSchool.create({
StudentId: studentId,
school: school
});
});
}).then(() => {
return res.redirect(redirectLink);
}).catch((err) => {
console.log('ERROR', err);
});
}
I've already looked at every other similar question I could find, but none of the solutions seem to work. I keep getting the following error.
Executing (default): INSERT INTO "StudentSchools" ("id","school","createdAt","updatedAt","StudentId") VALUES (DEFAULT,'sas','2017-07-22 00:41:49.763 +00:00','2017-07-22 00:41:49.763 +00:00',96) RETURNING *;
Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "StudentMajors" violates foreign key constraint "StudentMajors_StudentId_fkey"
at Query.formatError (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:296:14)
at Result.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:88:19)
at emitOne (events.js:96:13)
at Result.emit (events.js:188:7)
at Result.Query.handleError (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/query.js:163:8)
at Client.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/client.js:188:26)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at TLSSocket.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/connection.js:133:12)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:543:20)
Executing (default): SELECT "id", "email", "hash", "salt", "role", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."id" = 96;
Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "StudentSchools" violates foreign key constraint "StudentSchools_StudentId_fkey"
at Query.formatError (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:296:14)
at Result.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:88:19)
at emitOne (events.js:96:13)
at Result.emit (events.js:188:7)
at Result.Query.handleError (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/query.js:163:8)
at Client.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/client.js:188:26)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at TLSSocket.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/connection.js:133:12)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:543:20)
Any help would be much appreciated

Related

Connection error in Sequelize,Postgres,Nodejs

LOG:
///////////////
reject(new sequelizeErrors.ConnectionError(err));
^
ConnectionError [SequelizeConnectionError]: password authentication failed for user "postgres"
at Client._connectionCallback (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:142:24)
at Client._handleErrorWhileConnecting (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg/lib/client.js:305:19)
at Client._handleErrorMessage (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg/lib/client.js:325:19)
at Connection.emit (node:events:513:28)
at /home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg/lib/connection.js:114:12
at Parser.parse (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket. (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9) {
parent: error: password authentication failed for user "postgres"
at Parser.parseErrorMessage (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket. (/home/rishikesh/Desktop/Bug_tracker_test_1/Bug_Tracker_TOOL/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
///////////////sequelize
const {Sequelize,DataTypes} = require('sequelize');
const Upload_Model = require('./excel_upload_model');
const sequelize = new Sequelize('BugTracker','postgres','',{
host:'localhost',
dialect:'postgres'
});
const Sequelize_model = Upload_Model(sequelize,Sequelize);
sequelize.sync()
.then(()=>{
console.log(Database is ready)
})
module.exports = {
Sequelize_model
}
///////////// models
module.exports=(sequelize,DataTypes)=>{
return sequelize.define('excel_upload_model',{
id:{
type:DataTypes.INTEGER,
required:true,
primaryKey: true
},
unique_name:{
type:DataTypes.STRING,
},
enter code here
filename:{
type:DataTypes.STRING,
}
})
}

Unknown command when using promise-ftp in node

I'm trying to use a promise approach when interacting with my ftp server. I'm using the node package 'promise-ftp' v1.3.5.
The code I have is as follows:
const appFeedBuffer = Buffer.from('somedatastring', 'utf8');
var Client = require('promise-ftp');
var ftpClient = new Client();
ftpClient.connect({ host: 'myHost', user: 'me', password: 'mypassword' })
.then(() => {
return ftpClient.mkdir('newDir')
}).then(() => {
return ftpClient.put(appFeedBuffer, 'fileName')
}).then(()=> {
return ftpClient.rmdir('oldDir')
}).then(() => {
return ftpClient.rename('newDir', 'oldDir')
}).then(() => {
return ftpClient.end();
}).catch(err => {
console.log("something went wrong: " + err);
res.status(500).send('SERVER ERROR');
});
The problem is the put command - I get the error 'UNKNOWN COMMAND' which appears to come from the #icetee/ftp library, of which promise-ftp is a dependant. put is most certainly a valid command in the promise-ftp library!
Stack:
Unhandled rejection Error: Unknown command
at makeError (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:1128:13)
at Parser.<anonymous> (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:122:25)
at emitTwo (events.js:126:13)
at Parser.emit (events.js:214:7)
at Parser._write (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/parser.js:61:10)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Parser.Writable.write (_stream_writable.js:290:11)
at Socket.ondata (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:298:20)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
I can't find anyone else having this problem. What am I doing wrong?

Getting error while connecting database with nodejs

app.get('/', function (req, res) {
var config = {
//user: 'sa',
// password: '',
server: 'localhost',
database: 'TestDB',
debug: true
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from userInfo', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
//console.log(recordset);
sql.close();
});
});
});
I want to connect without user and password and Getting this error while executing through command prompt and there is some trusted connection query I can't find the please help
Error: Login failed for user ''.
at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:191:7)
at Connection.processLogin7Response (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1148:16)
at Connection.message (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1660:14)
at Connection.dispatchEvent (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:863:38)
at MessageIO.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:757:18)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:188:7)
code: 'ELOGIN',
originalError:
{ ConnectionError: Login failed for user ''.
at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:507:33)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at addChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:284:12)
at readableAddChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:271:11)
at Parser.Readable.push (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:238:10) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
at D:\Nodejs\UsersCreate\app.js:119:17
at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
GET / 200 36.940 ms - -
Please help me on this and want to connect this database using window authentication
You can try with:
options: {
trustedConnection: true
}
First install NPM Package,
npm install msnodesqlv8
Then change your config variable as,
const sql = require('mssql/msnodesqlv8')
var config = {
server: 'localhost',
driver: 'msnodesqlv8',
database: 'TestDB',
debug: true,
options: {
trustedConnection: true
}
};

trying to connect SQL using node js

app.get('/', function (req, res) {
var config = {
user: 'sa',
password: '',
server: 'localhost',
database: 'TestDB'
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from userInfo', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
console.log(recordset);
});
});
});
Getting this error in command prompt
{ Error: Failed to connect to localhost:1433 - Could not connect (sequence)
at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:191:7)
at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:14)
at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
code: 'ESOCKET',
originalError:
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:30)
at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at emitErrorNT (net.js:1284:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
code: 'ESOCKET' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
at D:\Nodejs\UsersCreate\app.js:118:17
at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
undefined
Please help me on this error and need navigation to rectify this error

Node Driver MongoDb - handleCallbaak is not defined instead of catch() firing

Using Node 8.* and MongoDb driver mongodb#2.2.31
I am testing the my code base to see how it behaves if my MongoDB ever crashes. The way I test is to turn the MongoDB daemon off.
The catch() statement is not executing. Instead I get the mongodb driver error ReferenceError: handleCallbaak is not defined when mongo.tempUsers.add(body, token, password) executes.
Why is this? Shouldn't the catch() execute?
TempUsers.prototype.add = function(body, token) {
const user = { "email" : body.email };
return this.collection.updateOne(user,
{ $set: {
"token": token,
"createdAt": new Date(),
}
},
{
writeConcern: true,
maxTimeMS: QUERY_TIME,
upsert: true
}
);
const confirmUser = (body, password) => {
const token = uuidv4();
const result = mongo.tempUsers.add(body, token, password);
result.then(() => sendConfirmationEmail(body, token)).catch(e => console.log(e));
};
UPDATE: How my own collections object is created:
var collections = {
tempUsers: false
};
function connect(dbURI) {
return MongoClient
.connect(dbURI)
.then(function(db) {
console.log(colors
.bold('MongoDB default connection open to ' + dbURI));
initDbListeners(db);
initCollections(db);
return db;
})
.catch(function(err) {
console.log(colors
.red(err));
});
}
function initCollections(db) {
collections.tempUsers = new TempUsers(db);
}
module.exports.collections = collections;
/home/one/github/dolphin/node_modules/mongodb/lib/collection.js:1057
if(err) return handleCallbaak(callback, err, null);
^
ReferenceError: handleCallbaak is not defined
at Object.c (/home/one/github/dolphin/node_modules/mongodb/lib/collection.js:1057:13)
at Store.flush (/home/one/github/dolphin/node_modules/mongodb/lib/topology_base.js:68:30)
at Server.reconnectFailedHandler (/home/one/github/dolphin/node_modules/mongodb/lib/server.js:290:18)
at emitOne (events.js:115:13)
at Server.emit (events.js:210:7)
at Pool.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/topologies/server.js:324:14)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/connection/pool.js:317:16)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at Socket.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/connection/connection.js:187:49)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
The mongodb node_modules/mongodb/lib/collection.js file on your system is different from the released file.
Remove the /home/one/github/dolphin/node_modules/mongodb directory from your project and run npm install again.

Resources