Angular js and spring mvc - angularjs

var Buyers = [
{
"first_name" : "Raj",
"last_name" : "Pilla"
},
{
"first_name" : "Ajit",
"last_name" : "Bambaras"
}
];
var Seller = {
"first_name" : "Simon",
"last_name" : "Mathew"
};
How we can post this data to controller using angular js post method also what we need to write in java controller..?

Yes, if you are trying to post multiple objects of the same type. You just need to use a list.
For e.g., in your case it will be :
public #ResponseBody String PostService(#RequestBody List<Seller> seller) {
}
However, if you're trying to send multiple objects of different types, you should instead use List to capture all the objects and then cast them as required.
For e.g.
public #ResponseBody String PostService(#RequestBody List<Object> requestObjects) {
Seller seller = (Seller) requestObjects.get(0);
Buyer buyer = (Buyer) requestObjects.get(1);
}

Related

Flutter : How to set datatype for each elements we want to upload to firebase database?

I want to upload a bunch of data into firebase database and set those data into a specific datatype.
In firebase database, we have string, number, boolean, map, array, null, timestamp, geopoint, and reference.
How do I get my flutter code to specify my database elements' types.
Future database() async {
return await userCollection.document(uid).setData({
string : ,
number : ,
boolean : ,
map : ,
array : ,
null : ,
timestamp : ,
geopoint : ,
reference: ,
});
}
setData takes a map as an argument, therefore you have to do the following:
Future database() async {
return await userCollection.document(uid).setData(
{
"name" : "john",
"age" : 50,
"email" : "example#example.com",
"address" : {
"street" : "street 24",
"city" : "new york"
}
})
This way you will add a string, integer, and a map to the database.

Mongodb data is not returned to nodejs api

I am able to connect to mongodb through my nodejs api but for some reason data is not returned. When i query mongodb from console , here is what i get :
MongoDB Enterprise > db.patients.find();
{ "_id" : ObjectId("58c9a5dd1eb8c7c1c68778ca"), "FName" : "n", "LName" : "ri", "Email" : "abc#yahoo.com", "phone" : "1234567890" }
here is the code from nodejs:
app.get('/api/patients',function(req,res){
Patient.getPatients(function(err,patients){
if(err){
throw err;
}
console.log(patients.length);
res.json(patients);
});
});
and this is getPatients method:
var Patient = module.exports = mongoose.model('patient',patientSchema);
// get patients
module.exports.getPatients = function(callback,limit){
Patient.find(callback);
}
But the browser always shows "[]" and no data. When i look at length of array ( console.log(patients.length)) in API , i get 0. Not sure why data is not returned.
Can someone pls point me what may be the issue ?
Update
Based on suggestions, I did following changes. I created 2 collections just to make sure that the collection exists. So i have patient and patients collections.
MongoDB Enterprise > db.patient.find();
{ "_id" : ObjectId("58cfe4551eb8c7c1c68778cc"), "FName" : "V", "LName" : "K", "Email" : "abc#yahoo.com", "phone" : "1234567890" }
{ "_id" : ObjectId("58cfe4601eb8c7c1c68778cd"), "FName" : "V1", "LName" : "K1", "Email" : "abc#yahoo.com", "phone" : "1234567890" }
MongoDB Enterprise > db.patients.find();
{ "_id" : ObjectId("58cfe42d1eb8c7c1c68778cb"), "FName" : "V", "LName" : "K", "Email" : "abc#yahoo.com", "phone" : "1234567890" }
Here is the code from nodejs file:
var mongoose = require('mongoose');
var patientSchema = mongoose.Schema({
FName: {
type : String
},
LName: {
type : String
},
Email: {
type : String
},
phone: {
type : String
},
},
{collection : 'patient'});
var Patient = module.exports = mongoose.model('patient',patientSchema);
// get patients
module.exports.getPatients = function(callback,limit){
console.log('test2');
Patient.find({}, function(err, patients) {
if (err) throw err;
// object of all the users
console.log(patients.length);
});
}
when i run this code, it prints 'test2' and '0' for me but not actual results. Can someone help with this ?
Are you sure you defined your schemas well and its exported very well... If yes then do something like this from your nodejs scripts
//get the model
var Patient = mongoose.model('patient',patientSchema);
// get all the patients
Patient.find({}, function(err, patients) {
if (err) throw err;
// object of all the users
console.log(patients);
});
I believe this should work
The answer above, while executing the find on the Patient model correctly, will not get the results back to your api response. I think if you change your getPatients code to this:
var Patient = module.exports = mongoose.model('patient',patientSchema);
module.exports.getPatients = function(callback) {
Patient.find({},
function(err, data) {
callback(err,data);
}
);
}
It will work. If that doesn't work, I'd want to see the entire code file to make sure the app.get can access the getPatients function - but you would get an error if that wasn't the case. So, I think this will work.
I figured my code was correct but connection string was incorrect.
Incorrect Connection string :
mongoose.connect('mongodb://localhost:27017/');
Correct connection string:
mongoose.connect('mongodb://localhost:27017/patients');

UpdateOperations embedded array

I have an structure like this:
{
_id: 123,
bs: [
{
_id: 234,
cs: [
{
_id: 456,
ds : [
{
_id: 678,
emails[
"email#gmail.com"
]
}
]
}
]
}
]
}
My classes in Morphia seems like this
#Entity
public class A {
#Id
private ObjectId id;
#Embedded
private List<B> bs;
}
public class B {
private ObjectId id;
#Embedded
private List<C> cs;
}
public class C {
private ObjectId id;
#Embedded
private List<D> ds;
}
public class D {
private ObjectId id;
#Embedded
private List<String> emails;
}
What I am trying to do is insert an email inside embedded array with Morphia without retrieve all element A and use updateFirst.
This is the query I am trying execute
Query<Event> query = this.basicDAO.createQuery();
query.criteria(Mapper.ID_KEY).equal(new ObjectId(aID));
query.criteria("bs.cs.id").equal(new ObjectId(cID));
query.criteria("bs.cs.ds.id").equal(dID);
UpdateOperations<Event> updateOps = this.basicDAO.createUpdateOperations().set("bs.cs.ds.$.emails", email);
this.basicDAO.update(query, updateOps);
I also read about this post Update an item in an array that is in an array with said that
$ operator does not work "with queries that traverse nested arrays".
So I tried something like:
D d = new D(dID);
C c = new C(new ObjectId(cID));
Query<Event> query = this.basicDAO.createQuery();
query.criteria(Mapper.ID_KEY).equal(new ObjectId(aID));
query.field("bs.cs").hasThisElement(c);
query.field("bs.cs.ds").hasThisElement(d);
UpdateOperations<Event> updateOps = this.basicDAO.createUpdateOperations().set("bs.cs.ds.emails", email);
this.basicDAO.update(query, updateOps);
However it still doesn't work. Any idea how solve this? The error message that I receive is cannot use the part ... to transverse the element
Based on your stand-in use case, I think you should "invert" your schema. Each document will represent a lecture and will be tagged with its theme, edition, and event:
{
"_id" : ObjectId("54da1ff0a9ce603a239c3075"),
"event" : "X0004",
"edition" : "A0002,
"theme" : "RT0005",
"votes" : 22
}
event, edition, and theme can be some kind of identifiers, and might be references to event, edition, and theme documents in other collections. To cast a vote for a particular lecture, just update it by _id:
db.test.update({ "_id" : ObjectId("54da1ff0a9ce603a239c3075") }, { "$inc" : { "votes" : 1 } })
While I don't know your full requirements, I think this is a better basic design given your example use case.

how to add a complextype object dynamically to an array

We have created an array of complextype(Carrier field) objects. See below metadata
{ shortName : 'Person',
namespace : 'Demo',
autoGeneratedKeyType : breeze.AutoGeneratedKeyType.Identity,
"dataProperties": [
{
"name": "carriers",
"complexTypeName":"Carrier:#Test",
"isScalar":false
}]
}
The Carrier entity is defined as below:
{
"shortName": "Carrier",
"namespace": "Test",
"isComplexType": true,
"dataProperties": [
{
"name": "Testing",
"isScalar":true,
"dataType": "String"
}
]
}
We have the following matching data for the above entities:
{
carriers: [
{
Testing : 'InputBox1'
},
{
Testing : 'InputBox2'
}
]
}
We are trying to dynamically add the complextype object(Carrier) to the above carriers array by using the following approach:
var test = {
"Testing" : "Test"
};
var result = manager.createEntity('Carrier', test);
The above code throws an exception(undefined is not a function) inside breeze.debug.js at line number 12457(see below code)
entity = entityType.createEntity(initialValues);
The exception is thrown since the complextype entity does not have 'createEntity' function in it.
What are we missing here?
Excellent question - Sorry I didn't have a chance to address this earlier.
When adding a complexType object you need to use the createInstance() method instead of the createEntity.
var thisEntityType = manager.metadataStore.getEntityType('Carrier');
var thisEntity = thisEntityType.createInstance(initialValues);
Basically you get the complexType and then create an instance of it using the values you want assigned. Keep in mind the initial values should be a hash object of course. Often I will include a helper function to do this for me like this -
function createComplexType(entityType, constructorProperties) {
var thisEntityType = manager.metadataStore.getEntityType(entityType);
var thisEntity = thisEntityType.createInstance(constructorProperties);
return thisEntity;
}

How to get unique/Distinct data from store?

i am using the extjs. i need to retrieve distinct data from store as per requirement. In my store contains single list not nested list in that have regions like AMERICAS , North Sea and SE Asia. these region it self have subRegion,vesselName and vesselType values. I need to retrive unique value based on region, bacasue it contains many duplicate records. I have tried like as below, but it is not working me. Can anybody tel me how to achieve ?. great appreciated. Thank you.
var vesselStore=Ext.getStore('VesselStatusReportStore');
var arr=new Array();
var obj;
vesselStore.each(function(rec,index)
{
obj=new Object();
if(rec.get('region')=='AMERICAS'){
obj.subRegionAmerica=rec.get('subRegion');
obj.vesselNameAmerica=rec.get('vesselName');
obj.vesselTypeAmerica=rec.get('vesselType');
}
if(rec.get('region')=='NorthSea'){
obj.subRegionNorthSea=rec.get('subRegion');
obj.vesselNameNorthSea=rec.get('vesselName');
obj.vesselTypeNorthSea=rec.get('vesselType');
}
if(rec.get('region')=='SE Asia'){
obj.subRegionSEAsia=rec.get('subRegion');
obj.vesselNameSEAsia=rec.get('vesselName');
obj.vesselTypeSEAsia=rec.get('vesselType');
}
arr.push(obj);
console.log(obj);
});
Json:
[ {
"region" : "AMERICAS",
"startDate" : null,
"subRegion" : "US",
"vesselName" : "Thoma-Sea � Hull #147",
"vesselType" : "PSV"
},
{
"region" : "AMERICAS",
"startDate" : null,
"subRegion" : "US",
"vesselName" : "Thoma-Sea � Hull #148",
"vesselType" : "PSV"
},
{
"region" : "AMERICAS",
"startDate" : null,
"subRegion" : "Mexico",
"vesselName" : "Thoma-Sea � Hull #148",
"vesselType" : "PSV"
}]
It looks like you want to use collect. http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-collect
vesselStore.collect('region')
This section doesn't make sense:
obj=new Object();
if(rec.get('region')=='AMERICAS'){
obj.subRegionAmerica=rec.get('subRegion');
obj.vesselNameAmerica=rec.get('vesselName');
obj.vesselTypeAmerica=rec.get('vesselType');
}
if(rec.get('region')=='NorthSea'){
obj.subRegionNorthSea=rec.get('subRegion');
obj.vesselNameNorthSea=rec.get('vesselName');
obj.vesselTypeNorthSea=rec.get('vesselType');
}
if(rec.get('region')=='SE Asia'){
obj.subRegionSEAsia=rec.get('subRegion');
obj.vesselNameSEAsia=rec.get('vesselName');
obj.vesselTypeSEAsia=rec.get('vesselType');
}
arr.push(obj);
You are basically saying "Whatever the region is, copy the record to obj, then add that obj to my array".
I believe you meant something more along these lines:
var vesselStore=Ext.getStore('VesselStatusReportStore');
var iRegions = [];
vesselStore.each(function(rec,index)
{
var iRegionName = rec.get('region');
// Make sure theres a array item with the region name,
// if not create a blank array;
iRegions[ iRegionName ] = iRegions[ iRegionName ] || [];
// Add the record to its corresponding array item.
// (If you don't want the full record, you can just get the
// fields individually like you did in your code).
iRegions[ iRegionName ] = rec;
}
console.log( iRegions );

Resources