Mongoose require model in model - express

i want to use a static-method from another model in a model. But when i require the other model and call the function:
TypeError: Cannot read property 'isCalculated' of undefined
so.. is it not possible to require a model in another model? :(
The models are in the same folder, so I call
var Confirmation = require("./confirmation").Confirmation;
and export in confirmation:
module.exports = {
Confirmation: mongoose.model('Confirmation', confirmationSchema)
};
Thanks :)
Update: The code:invoice.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var autoIncrement = require('mongoose-auto-increment');
var Moment = 'Moment';
var moment = require('moment');
var Currency = mongoose.Types.Currency;
var Confirmation = require("./confirmation.js");
autoIncrement.initialize(mongoose.connection);
var invoiceSchema = new Schema({...}); //invoiceSchema
invoiceSchema.statics.insert= function(invoice, cb) {
....
Confirmation.isCalculated(..) //error here
....
}
module.exports = mongoose.model("Invoice", invoiceSchema);
And the code of: confirmation.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Invoice = require("./invoice.js");
var confirmationSchema = new Schema({...});
confirmationSchema.statics.isCalculated = function(pid, aid, cb) {...};
module.exports = mongoose.model('Confirmation', confirmationSchema)

My guess would be that because of your circular imports (confirmation.js requires invoice.js which requires confirmation.js...), the Confirmation model in your invoice.js isn't materialized properly.
I think you should remove the circular imports altogether and dynamically get a reference to "the other" model using mongoose.model(), like so:
invoiceSchema.statics.insert = function(invoice, cb) {
....
mongoose.model('Confirmation').isCalculated(..)
....
}

Related

SQLite3 Database is not a constructor

So in my project I am trying to gather simple Discord username and unique identifier from discord and store it in SQLite database file. I get the error:
` let userDB = new sqlite.Database('./disco.db', sqlite.OPEN_READWRITE);
^
TypeError: sqlite.Database is not a constructor`
Here is my code in my index.js
// Requirements
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
const ServList = client.guilds.cache.size;
const sqlite = require('sqlite3').verbose();
require('dotenv').config()
//client login function
client.login(process.env.TOKEN);
// Start up Check list
client.once('ready', () => {
//Log and Set Status
console.log('Bot Online');
client.user.setActivity(`Proudly in ${client.guilds.cache.size} servers`, {
type: "WATCHING",
}, 60000);
//Database Initialization
let userDB = new sqlite.Database('./disco.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
});
Here is my code for the command that is creating the error:
const Discord = require('discord.js');
const sqlite = require('sqlite3').verbose();
module.exports = {
name: 'create',
description: "Create your account!",
use(message, args, client, sqlite){
// Data to Add
let userDB = new sqlite.Database('./disco.db', sqlite.OPEN_READWRITE);
userDB.run(`CREATE TABLE IF NOT EXIST usersInfo(userID INTEGER NOT NULL, uNameR TEXT NOT NULL)`);
let userID = message.author.id;
let uName = message.author.tag;
let uQuery = `SELECT * FROM usersInfo WHERE userID = ?`;
userDB.get(uQuery, [userID], (err, row) => {
if (err) {
console.log(err);
return;
}
if (row === undefined){
userDB.prepare(`INSERT INTO usersInfo VALUES(?,?)`);
insertdata.run('userID, uName');
insertdata.finalize();
userDB.close();
} else {
let userID2 = row.userID;
let yName = row.uNameR;
console.log(yName, userID);
}
});
message.channel.send('success');
}
}
Edit: Your question has been identified as a possible duplicate of another question. If the answers there do not address your problem, please edit to explain in detail the parts of your question that are unique.
The suggestion solution does not work for me as the suggested answer utilizes mySQL while I use SQLite3, Not only that but the suggested answer attempts to connect to a hosted database while mine is local.

Odoo Missing Dependencies: web.ControlPanelMixin

I'm trying to install some module In Odoo 13 Community. The dependency 'web.ControlPanelMixin' is missing, I'm trying to fing the problem, so far I got no succes.
The JavaScript Code:
odoo.define('crm_dashboard.dashboard', [
'web.core',
'web.framework',
'web.session',
'web.ajax',
'web.ActionManager',
'web.view_registry',
'web.Widget',
'web.AbstractAction',
'web.ControlPanelMixin'
], function (require) {
"use strict";
var core = require('web.core');
var framework = require('web.framework');
var session = require('web.session');
var ajax = require('web.ajax');
var ActionManager = require('web.ActionManager');
var view_registry = require('web.view_registry');
var Widget = require('web.Widget');
var AbstractAction = require('web.AbstractAction');
var ControlPanelMixin = require('web.ControlPanelMixin');
var QWeb = core.qweb;
...
});
I tried searching for where is 'web.ControlPanelMixin' declared:
[odoo-13.0]$ grep -rnw ./ -e 'ControlPanelMixin'
./doc/reference/javascript_reference.rst:2221: var ControlPanelMixin = require('web.ControlPanelMixin');
./doc/reference/javascript_reference.rst:2224: var ClientAction = AbstractAction.extend(ControlPanelMixin, {
./doc/reference/javascript_reference.rst:2262:- add ControlPanelMixin in the widget:
./doc/reference/javascript_reference.rst:2266: var ControlPanelMixin = require('web.ControlPanelMixin');
./doc/reference/javascript_reference.rst:2268: var MyClientAction = AbstractAction.extend(ControlPanelMixin, {
./doc/reference/javascript_reference.rst:2277: var SomeClientAction = Widget.extend(ControlPanelMixin, {
./mymodules/odoo_crm_dashboard/static/src/js/crm_dashboard.js:11: 'odoo.web.ControlPanelMixin'
./mymodules/odoo_crm_dashboard/static/src/js/crm_dashboard.js:25:var ControlPanelMixin = require('web.ControlPanelMixin');
./mymodules/odoo_crm_dashboard/static/src/js/crm_dashboard.js:31:var CRMDashboardView = AbstractAction.extend(ControlPanelMixin, {
Thank you!
I think there is a mistake:
odoo.define('crm_dashboard.dashboard', [
'web.core',
'web.framework',
'web.session',
'web.ajax',
'web.ActionManager',
'web.view_registry',
'web.Widget',
'web.AbstractAction',
'web.ControlPanelMixin'
]
It is web.ControlPanelMixin not odoo.web.ControlPanelMixin
You can review this documentation
ControlPanelMixin was removed in Odoo 13.0.
Instead of extending from it, objects can now use hasControlPanel: true. See the Odoo source code for an example.

function + module.exports

I have a problem with module.export on titanium. I tried following https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium but it doesn't work at all.
I have 2 little pieces of code. App.js:
var fenetreBase = Titanium.UI.createWindow({fullscreen:true,backgroundColor:"white",exitOnClose:true});
fenetreBase.open();
var vueimage = new (require('UI/viewimage'))();
vueimage.test();
fenetreBase.add(vueimage);
and viewimage.js in the folder UI.
function viewimage() {
var lavue = Ti.UI.createView({backgroundColor:'red' });
var item =...
lavue.add(item...);
return lavue;
}
viewimage.prototype.test = function() {
Ti.API.info("test");
};
module.exports = viewimage;
I have an error saying
Object #<view> has no method 'test' in app.js vueimage.test()
In my mind, I follow the example of "Instantiable Objects" in the wiki above but I may have not understand something. I expect I made a stupid mistake. I tried many other things, each uglier than other and it doesn't work anyway.
Can somebody tell me where the mistake is?
your mistake is assuming that you have an instance of viewimage when you run:
var vueimage = new (require('UI/viewimage'))();
you are getting an instance of
var lavue = Ti.UI.createView({backgroundColor:'red' });
which doesn't have a test property.
Perhaps you could use an object like this instead:
function viewimage() {
var result = {};
var lavue = Ti.UI.createView({backgroundColor:'red' });
var item =...
lavue.add(item...);
result.lavue = lavue;
result.test = function() {
Ti.API.info("test");
};
return result;
}
EDIT
In your App.js:
var vueimage = new (require('UI/viewimage'))();
vueimage.test();
fenetreBase.add(vueimage.lavue);

knockout mapping to be done only first time

I am generating the knockout mapping from server side view model using below
var bindData2ViewModel = function (data) {
var rdata = ko.toJSON(data);
ko.mapping.fromJSON(rdata, {}, vm.model());
ko.applyBindings(vm);
};
var CustomerViewModel = function () {
var self = this;
self.model = ko.observable({});
return { model: self.model };
};
var vm = new CustomerViewModel();
now there is another call which is giving me the data... i just want to bind that data to the client side viewmodel without changing the binding... how to do that?
var rebindData2ViewModel = function (data) {
var rdata = ko.toJSON(data);
vm.model.set(rdata);
ko.applyBindings(vm);
};
tried above but not working... what is the correct way to do this?
basically to rebind the data to the existing model.. you just need to set the data using the angular brackets.. no need of json etc... because data should itself return as jsonresult
var rebindData2ViewModel = function (data) {
vm.model(data);
};

how to implement function from another .js file in Titanium

how to use functions/methods from another .js file in titanium?
eg
Utils.js
var db = Titanium.Database.open('myinfodb');
function addIntoDb(name) {
db.execute('INSERT INTO info (name) VALUES(?)', name);
Ti.Ti.API.info(name+' Added to db');
}
function getFromDb() {
var holddatavar = db.execute('SELECT name FROM info');
return holddatavar;
}
db.close();
how to use this in my current js file?
db.js
// creates your database if necessary
var db = Titanium.Database.open('myinfodb');
db.execute('CREATE TABLE IF NOT EXISTS [info] (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
db.close();
var addIntoDb = function(name) {
var db = Titanium.Database.open('myinfodb');
db.execute('INSERT INTO info (name) VALUES(?)', name);
Ti.Ti.API.info(name+' Added to db');
db.close();
}
exports.addIntoDb = addIntoDb; // <=== This exposes the function to another file
// This function would remain accessible only to the file because we didn't export the function
function getFromDb() {
var holddatavar = db.execute('SELECT name FROM info');
return holddatavar;
}
Then you would use it in other JavaScript files to access it like so:
var db = require('/mypath/db'); // NO .js extension on this
db.addIntoDb('Pravin');
Got it.
just
Ti.include('/ui/common/Utils.js');
and
Utils.js will be
addStudent= function addIntoDb(name) {
var db = Titanium.Database.open('myinfodb');
db.execute('INSERT INTO info (name) VALUES(?)', name);
Ti.API.info(name+' Added to db');
}
getStudent=function getFromDb() {
var db = Titanium.Database.open('myinfodb');
var holddatavar = db.execute('SELECT name FROM info');
return holddatavar;
}
If you are not using commonJS then you can do it simply including that file i mean create one file for such function and include that file in your current.js file and call that function.
var All = require('ui/common/All');
Tree = require('ui/common/Tree');
EBOM = require('ui/common/E-BOM');
MBOM = require('ui/common/M-BOM');
SBOM = require('ui/common/S-BOM');
//create object instance
var self = Ti.UI.createWindow({
title:'Products',
exitOnClose:true,
navBarHidden:true,
backgroundColor:'#ffffff',
/////////////////////////////////////////////////////////////////////////////
activity: {
onCreateOptionsMenu: function(e) {
var menu = e.menu;
var menuItem = menu.add({ title: "C-BOM", icon: 'Arrow-Hover.jpg' });
//menuItem.setIcon("Arrow-Hover.jpg");
menuItem.addEventListener("click", function(e) {
var all = new All();
self.add(all);
});
(.....)
Best is to make subfolder in /lib folder like /Utils and put there a file in /Utils/Utils.js.
Then in index.js controller add global variable like :
Alloy.Globals.Helpers=Ti.include('/Utils/Utils.js')
When you do it like this your file is now accessible in any controller you make.
Note this is for Alloy but it is almost the same for classic Titanium.