When I store a specific domain object (grom) in my grails-2.1.1 Application, I get the following exception:
java.sql.SQLException: No value specified for parameter 1
I use mysql as my database. I just want to persist my domain object. I tried to replace the logic inside my controller by
def hauptprojektInstance = Hauptprojekt.get(params.id)
hauptprojektInstance.save(flush: true)
but even this query fails with the same error.
The Hauptprojekt-domain model has a OneToMany and a OneToOne relation to other objects.
Related
im trying to use seneca for microservice stuffs. im new and inexperience in this field. i have a problem where my json object received is undefined but it shows in the console.log.
console.log("company:", body.data.company)
company = body.data.company
and this is the error result. it shows that it the company clearly contains a value and not a null
company: BST
{"notice":"seneca: Action cmd:addMember,role:client failed: company is not defined.","code":"act_execute","err":{"eraro":true,"orig":{},
"code":"act_execute","seneca":true,"package":"seneca","msg":"seneca: Action cmd:addMember,role:client failed: company is not defined. ...
From the above provided information i can tell that company field not defined means it is not present into body.data
try printing body on console and check the whole incoming data.
In our project, we are "cloning table data" and "subsequent tables" in the hierarchy. For this, in the domain objects, we are writing a "clone()" method, creating an object of the class and then setting the properties in the created object. What we are doing in code is like this (this is the clone object of a "dummy Book.groovy class" representing book table in database)
public Book clone(def newParent) {
def clonedBook = new Book(properties)
clonedBook.parent=newParent
clonedBook.<childObject>=<childObject>*.clone(clonedBook)
return clonedBook
}
The "clone()" method of Book is called from the clone method of the domain class of parent table, and it in turn calls "clone()" method of child domain class. In database the foreign keys are made as ON DELETE NO ACTION
So while running this way, I was getting integrity constraint violation exception, which got resolved by changing the foreign keys from "ON DELETE NO ACTION to ON DELETE CASCADE". Then I started getting "HibernateOptimisticLockingFailure exception". This was also solved by not passing 'properties' in constructor of "clonedBook", but setting the properties of Book.groovy explicitly in "clonedBook". Example, the "clone()" method is
public Book clone(def newParent) {
def clonedBook = new Book() //properties not passed here
clonedBook.name = name //explicitly set
clonedBook.price = price //explicitly set
...
}
But I was unable to find why the problem was solved this way, or what was wrong the previous way. I am using "Grails 2.4.4" and MySQL database
How do you query MongoDB using mongoose with node.js? I have it to where I can insert my JSON object to my DB, but all the ways I have tried to return the JSON object from the DB return null or just information about the database.
Is there some good method using mongoose to be able to query the database similar to the method:
var cursor = db.collection.find()
var JSONobject = cursor.next()
Here's what is in my code right now:
mongoose.connect('mongodb://localhost/myDB');
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
var cursor = mongoose.connection.db.contents.find();
console.log(cursor.next());
This throws an error at the line :
var cursor = mongoose....
claiming 'cannot call method 'find' of undefined'.
Note, that my collection 'contents' does in fact exist, and it contains one JSON document. I know this because I manually navigated to the collection using the mongo shell.
Edit: I am open to alternative methods to querying the database. I simply just want to be able to return JSON objects from my DB one at a time, while keeping track of where the client is at in the database.
One method to query mongoDB using mongoose is as follows:
Content.findOne().exec(function(err,docs){console.log(docs)});
Docs contains the JSON object. Access its attributes like any other object.
Note, this method uses asynchronous call backs. As such, you can't store the JSON object docs to a variable in order to use the JSON document information outside of the function. Therefore, you need to perform whatever actions needed with the JSON docs object information inside of the function.
For example, I was needing the query information to provide the filepath for my get api. As such, the result was as follows:
//get
app.get('/api/media/', function(req,res){
Content.findOne().exec(function(err,docs){res.sendFile(path.join(__dirname, '/api/media/', docs.filename))});
});
Note, Content is the model of my schema, and one of its parameters is filename.
I am making a simple REST API in front of a NoSQL database that stores records as documents similar to JSON (but not exactly the same). Each record has some fields, including id for the database, and also including some derived fields, like dateCreated.
Any time I GET anything, I want to return the objects with all the fields.
// GET /users returns an array of these in JSON
// [{id:"xxx", name:"Bobby", dateCreated:"YYYY-MM-DD"]
data User = User { id :: String, name :: String, dateCreated :: XXX }
But any time I POST or PUT anything, they client should send an object with the id field and any derived fields missing. The database is responsible to create the id when I save it, and I would create some derived fields
// POST /users would need you to post only the name.
// {name:"Henry"}
data PartialUser = PartialUser { name :: String }
If resource represents objects of type User, what should I call the thing client is sending to me? Would you make all the derived fields Maybe values? Or would you create a second object called PostedUser or something?
It can be many things:
a request body
the representation of the intended resource state of the client
a command DTO which you can send to the domain logic in order to process it by CQRS
I would make it CreateUser command, but if you don't want to use CQRS and DDD, then you would probably call it as PartialUserRepresentation, or you don't create a data structure, just use the properties to create a new User entity. Ofc. if you use entities.
So I would say it depends on the architecture of your system.
As part of a web application users can upload files of data, which generates a new table in a dedicated MySQL database to store the data in. They can then manipulate this data in various ways.
The next version of this app is being written in CakePHP, and at the moment I can't figure out how to dynamically assign these tables at runtime.
I have the different database config's set up and can create the tables on data upload just fine, but once this is completed I cannot access the new table from the controller as part of the record CRUD actions for the data manipulate.
I hoped that it would be along the lines of
function controllerAction(){
$this->uses[] = 'newTable';
$data = $this->newTable->find('all');
//use data
}
But it returns the error
Undefined property:
ReportsController::$newTable
Fatal error: Call to a member function
find() on a non-object in
/app/controllers/reports_controller.php
on line 60
Can anyone help.
You need to call $this->loadModel('newTable') to initialize it properly. Cake needs to initialize $this->newTable properly, and call all the callbacks.
Of course, you don't need $this->uses[] = 'newTable';, that doesn't do anything except add another value to the $uses array.
try:
function controllerAction() {
$data = ClassRegistry::init('ModelNameForNewTable')->find('all');
}
If your table is called 'new_tables', your model name should be 'NewTable'