Catch is not a function odoo - javascript

why am I returning a Uncaught TypeError: creacteAccount(...).then(...).catch is not a function? Is .catch() is unacceptable? Please bear with me since I am new with these technologies :)
function creacteAccount(db_name, contact, email) {
if (!(db_name > "" && contact > "" && email > "")) {
return Promise.reject("One of the parameters is empty");
}
return session.rpc('/custom/createdb', {db_name: db_name})
.then(function () {
console.log("Database created successfully");
return session.rpc('/custom/initdb', {db_name: db_name});
}).then(function () {
console.log("Database initialized successfully");
return session.rpc('/custom/installapps', {db_name: db_name});
}).then(function () {
console.log("Apps installed successfully");
return session.rpc('/custom/createaccount', {
db_name : db_name,
contact_name: contact,
email_from: email
});
}).then(function () {
console.log("User account created successfully");
});
}
creacteAccount(db_name, contact, email).then(function () {
console.log("Successfully creating your account")
}).catch(function (err) {
alert("Could not create account! (" + err + ")");
});
This is the logs: https://jpst.it/2p-nG

Related

Object is possibly undefined in TypeScript

I did use sfDoc !== undefined, but still I'm getting the error of object is possibly undefined. Am I doing anything wrong here?
return database.runTransaction(function (transaction) {
return transaction.get(sfDocRef).then(sfDoc => {
if (!sfDoc.exists) {
throw "Document does not exist!";
}
if (sfDoc !== undefined) {
var usedCount = sfDoc.data().usedCount + 1;
transaction.update(sfDocRef, { usedCount: usedCount });
}
return transaction;
});
}).then(function () {
console.log("Tag field changed!");
return true;
}).catch(function (error) {
console.log("Error in changing Tag field: ", error);
return false;
});
Try this example. Check for the sfDoc and return transaction.update, So that then wait to resolve the promise. According to document, you don not has to check for sfDoc. It will be always defined.
return database
.runTransaction(function (transaction) {
return transaction.get(sfDocRef).then((sfDoc) => {
if (sfDoc && sfDoc.exists) {
var usedCount = sfDoc.data().usedCount + 1;
return transaction.update(sfDocRef, { usedCount: usedCount });
} else {
throw "Document does not exist!";
}
});
})
.then(function () {
console.log("Tag field changed!");
return true;
})
.catch(function (error) {
console.log("Error in changing Tag field: ", error);
return false;
});

Creating a user with POST , but it's not working correctly

I'm creating a new user with POST and the program tells me it's created correctly , but when I check with GET if the user was created , it creates an empty {} entry. What can be wrong here?
app.get(BASE_API_PATH + "/contacts", (req, res) => {
console.log(Date() + " - GET /contacts");
db.find({}).toArray((err, contacts) => {
if (err) {
console.error("Error accesing DB");
res.sendStatus(500);
return;
}
res.send(contacts.map((c) => {
delete c._id;
return c;
}));
});
});
app.post(BASE_API_PATH + "/contacts", (req, res) => {
console.log(Date() + " - POST /contacts");
var contact = req.body;
if (!contact) {
console.log("warning : new Get req");
res.sendStatus(400);
}
db.find({ "name": contact.name }).toArray((err, contacts) => {
if (err) {
console.log("error accesing db");
res.sendStatus(400);
}
if (contacts.length > 0) {
console.log("warning");
res.sendStatus(409);
}
else {
db.insert(contact);
res.sendStatus(201);
}
});
});

Firebase transaction not working

I am calling from an angular app, using angularfire2, this.afDb.database is the db instance
const downloadsRef = this.afDb.database.ref('research_reports-published/' + rrid + '/' + field);
downloadsRef.transaction(function(fieldval) {
if (fieldval) {
fieldval = fieldval + 1;
}
return fieldval;
},
function(error, committed, snapshot) {
if (error) {
console.log('Transaction failed abnormally!', error);
} else if (!committed) {
console.log('We aborted the transaction (because ada already exists).');
} else {
console.log('User ada added!');
}
console.log('Adas data: ', snapshot.val());
}).then(function() {
console.log('Transaction successfully committed!');
}).catch(function(error) {
console.log('Transaction failed: ', error);
});
The code just silently prints
Adas data: oldvalue
Transaction successfully committed
and exits
Found the issue in above code. It should be
if (fieldval != null) {
fieldval = fieldval + 1;
}
as fieldval=0 also makes it false

REStful API promise calling another promise

I am refactoring my code using promises. I am running into a problem. I have two API routes. First one is api.js and the second is account.js. I also have 4 controllers(CommentController, ZoneController, ProfileController, AccountController) .
CommentController, ZoneController, and ProfileController share the same API route(api.js).
account.js uses AccountController. But AccountController's method uses ProfileController's method.
I ended up having Promise calling another Promise, but I am not returning data properly. It is leaving the server hanging. How can I return data when one Promise is calling another Promise?
Basically account.js is calling AccountController.js that has a method that calls ProfileController.js, but both AccountController and ProfileController are refactored to Promise. I am not getting data back. Please help.
AccountController.js
var ProfileController = require('./ProfileController');
module.exports = {
currentUser: function(req) {
return new Promise(function(resolve, reject) {
if (req.session == null) {
reject({message: 'User not logged in'});
return;
}
if (req.session.user == null) {
reject({message: 'User not logged in'});
return;
}
ProfileController.findById(req.session.user, function(err, result) {
if (err) {
reject({message: 'fail'});
return;
}
resolve(result);
return;
});
});
}
ProfileController.js
findById: function(id) {
return new Promise(function(resolve, reject){
Profile.findById(id, function(err, profile){
if(err){
reject(err);
return;
}
resolve(profile);
return;
});
})
},
account.js
router.get('/:action', function(req, res, next) {
var action = req.params.action;
if (action == 'logout') {
req.session.reset();
res.json({
confirmation: 'success',
message: 'Bye!'
});
return;
}
if (action == 'login') {
res.json({
confirmation: 'success',
action: action
});
return;
}
if (action == 'currentuser') {
AccountController.currentUser(req)
.then(function(result){
res.json({
confirmation: 'success',
user: result
});
return;
})
.catch(function(err){
res.json({
confirmation: 'fail',
message: err.message
});
return;
});
}
});
you should modify AccountController.js
AccountController.js
var ProfileController = require('./ProfileController');
module.exports = {
currentUser: function(req) {
return new Promise(function(resolve, reject) {
if (req.session == null) {
reject({message: 'User not logged in'});
return;
}
if (req.session.user == null) {
reject({message: 'User not logged in'});
return;
}
ProfileController.findById(req.session.user).then(
function (profile){
resolve(profile);
},function (err) {
reject(err);
});
}

Uncaught TypeError: Cannot read property 'get' of undefined error

I am trying to use either the email or username of the User class to login. However, when I query for the email and try to do
object.get("username")
I get the error message above. Strangely, when I test the helper function getUsername in the debugger it works fine
function getUsername(email) {
var query = new Parse.Query(Parse.User);
query.equalTo('email', email);
query.first({
success: function(object) {
console.log(object.get("username"));
return object.get("username");
},
error: function(user, error) {
console.log("no email");
}
});
}
function signIn(usernameOrEmail, password) {
//if not email sign in with username
if (usernameOrEmail.indexOf("#") == -1) {
Parse.User.logIn(usernameOrEmail, password, {
success: function(user) {
console.log("Logged in!");
},
error: function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
//query for username from email and signin
else {
var username = getUsername(usernameOrEmail);
Parse.User.logIn(username, password, {
success: function(user) {
console.log("Logged in!");
},
error: function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
}
query.first is an asynchronous function. To use the values it returns, you have to do it in the success: callback function. So pass a callbak to getUsername that performs the login.
function getUsername(email, callback) {
var query = new Parse.Query(Parse.User);
query.equalTo('email', email);
query.first({
success: function(object) {
if (object) {
console.log(object.get("username"));
callback(object.get("username"));
} else {
console.log("email not found");
}
},
error: function(user, error) {
console.log("no email");
}
});
}
function signIn(usernameOrEmail, password) {
//if not email sign in with username
if (usernameOrEmail.indexOf("#") == -1) {
Parse.User.logIn(usernameOrEmail, password, {
success: function(user) {
console.log("Logged in!");
},
error: function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
//query for username from email and signin
else {
getUsername(usernameOrEmail, function(username) {
signIn(username, password);
});
}
}

Categories