How do I insert into the database the array structure of objects as a string?
$scope.lists = [
{
label: "Parcerias Principais",
allowedTypes: ['p1'],
class: "b1",
people: []
},
{
label: "Atividades Principais",
allowedTypes: ['p2'],
class: "b2",
people: []
}
];
$scope.salvar = function(){
$http.post('link', {
aluno: 1,
tarefa: $scope.lists,
data: new Date(),
dataultima: new Date(),
curso: 82}).then(function(resultado){
$ret = resultado.data;
console.log($ret);
});
}
expected outcome:
enter image description here
I want to insert my direct array into the SQLSERVER database in this way.
Try JSON.stringify as used below :
$scope.salvar = function(){
$http.post('link', {
aluno: 1,
tarefa: JSON.stringify($scope.lists),
data: new Date(),
dataultima: new Date(),
curso: 82}).then(function(resultado){
$ret = resultado.data;
console.log($ret);
});
}
Related
I am trying to create a nested JSON object using NodeJS. I am getting data from the JIRA software server and trying to map to my application's schema. Below is my application's schema.
schema:
Product:
_id: ObjectId
categories: [Category]
backlog: [Section]
Category:
_id: ObjectId
name: String
color: String
Section:
_id: ObjectId
title: String
backlogItems: [BacklogItem]
BacklogItem:
_id: ObjectId
productId: ObjectId
title: String
description: String
category: Category
acceptanceCriteria: [AcceptanceCriterion]
tasks: [Task]
notes: [Note]
I want to create one category for tasks. For example, if there are two or more issues from JIRA of type "Epic", I want to create only one category and then I want to create an array of BacklogItems but it is giving me below error:
categories: [Category], Category is not defined
Below is what I am trying to do:
function parseResponse(body) {
var data = JSON.parse(body);
result.product = {
id: ObjectID(),
categories: [Category],
backlog: [Section]
};
for (var i = 0; i < data.issues.length; i++) {
var color;
var categoryName;
switch (data.issues[i].fields.issuetype.name) {
case "Story":
color = "green";
break;
case "Epic":
color = "purple";
break;
case "Task":
color = "blue";
break;
case "Bug":
color = "red";
break;
}
var uniqeCategory = [
...new Set(data.issues.map(x => x.fields.issuetype.name))
];
if (uniqeCategory[i] !== data.issues[i].fields.issuetype.name) {
categoryName = uniqeCategory[i];
} else {
categoryName = data.issues[i].fields.issuetype.name;
}
console.log(uniqeCategory);
result.product.backlog.push({
id: ObjectID(),
title: "importer",
backlogItem: []
});
result.product.backlog.backlogItem.push({
id: data.total,
productId: result.product.id,
title: data.issues[i].fields.summary,
description: data.issues[i].fields.description,
category: data.issues[i].fields.issuetype.name,
acceptanceCriteria: [],
tasks: [],
notes: []
});
Can somebody try to tell me what I am doing wrong and How can I create such an object?
PS: I just started working with NodeJS and trying to learn it by doing it.
After some efforts, I tried to create an object. Below is my code.
for (var j = 0; j < issue.fields.attachment.length; j++) {
var note = {
_id: ObjectID(),
createdAt: issue.fields.attachment[j].created,
content: issue.fields.attachment[j].filename,
attachmentUrl: issue.fields.attachment[j].content,
attachmentPreviewUrl: issue.fields.attachment[j].thumbnail
};
notesArray.push(note);
}
/* Creating seaparate notes by taking comments from each JIRA issue and put it in form of note */
for (var k = 0; k < issue.fields.comment.comments.length; k++) {
var note = {
_id: ObjectID(),
createdAt: issue.fields.comment.comments[k].created,
content: issue.fields.comment.comments[k].body,
attachmentUrl: "",
attachmentPreviewUrl: ""
};
notesArray.push(note);
}
/* Creating backlog items */
result.product.backlog.push({
_id: ObjectID(),
productId: result.product._id,
title: issue.fields.summary,
description: issue.fields.description,
category: issue.fields.issuetype.name,
acceptanceCriteria: [],
tasks: [],
notes: notesArray
});
console.log("final", result.product);
});
So I have a problem with my var array to add a new chapter, how would I go about this would I have to do this:
array.push({
chapter: [
{
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
}
]
});
RiTest.ts
import * as mongoose from 'mongoose';
const Scheme = mongoose.Schema;
export const RiTestScheme = new Scheme({
novelName: String,
novelAuthor: String,
novelCoverArt: String,
novelTags: Array,
chapters: [
{
id: Number,
title: String,
content: String,
authorNotes: String
}
]
});
export class RiTestController {
public addChapter(callback: (data) => void) {
var chapterInfoModel = mongoose.model('ChaptersTest', RiTestScheme);
var array = [
{
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
}
];
let newChapterInfo = new chapterInfoModel(array);
newChapterInfo.save((err, book) => {
if (err) {
return callback(err);
} else if (!err) {
return callback(book);
}
});
}
}
This doesn't work, var array doesn't get saved into let newChapterInfo = new chapterInfoModel(array); what I am trying to do add another chapter to array.chapter but the array doesn't get recognized in the chapterInfoModel() how would I fix this array and add an item to the array to create a new entry into this existing collection
thank for taking your time to answer my question.
You are trying to insert array of document to your collections, That the reason its not inserting into your collection.
Document.prototype.save() will insert only one document to your collection depends on your definition. So to insert chapter here is the code below,
//array as Object
var array = {
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
};
//Push to your chapter array
array.chapter.push({
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
});
let newChapterInfo = new chapterInfoModel(array);
I got an array from database as
[
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
Now my required answer is that should be in the following format,
[["abc",100],
["xyz",150]]
But after some coding I got an array of Array as shown below in the console
[0:[0:"abc",1:100]
1:[0:"xyz",1:150]]
Before question down / marking duplicate please read my requirement, and if its there then mark it and please post that link as per my required solution so i can get my solution from there
So any help will be great help....
You can do like this:
data = [
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
var array =[]
data.forEach(function(item){
var tmpArray = []
tmpArray.push(item.id.unitno,item.amount)
array.push(tmpArray)
})
Now you will get your required data in the array.
I don't know how you got this below code, but that's invalid Array
[0:[0:"abc",1:100] 1:[0:"xyz",1:150]]
For your desired output you can do it with JavaScript, no need angular. Just run a simple for loop.
var temp1 = [{
id: {
unitno: 'abc'
},
amount: 100
}, {
id: {
unitno: 'xyz'
},
amount: 150
}];
var eee = [];
temp1.forEach(function(el) {
eee.push([el.id.unitno, el.amount])
});
console.log('eee:', eee);
Hope this is what you needed.
var arr = [{
id : { unitno: 'abc' },
amount: 100
},{
id : { unitno: 'xyz' },
amount: 150
}];
var result = [];
for (var i = 0; i < arr.length; i++) {
var newArray = [];
newArray.push( arr[i]["id"]["unitno"] );
newArray.push( arr[i]["amount"] );
result.push( newArray );
}
console.log( result );
Let's assume I have the following model in mongoose:
var Product = new Schema({
eanCode: String,
brandName: String,
productNameNl: String,
sex: String,
suggestedRetailPrice: Number
})
How can I write a function to query this model using an array with parameters? I want a generic function which can get anything from this model using an array of filter parameters. For example:
var filterArray = [
sex: "gents",
brand: "brandName"
];
var fieldsArray = ["sex", "brand", "productNameNl", "eanCode"];
var getBrandGentsProducts = getProducts(filterArray);
function getProducts(fields, filter){
Product.Find({fields}, {filter}).exec(function(err, products){
return products;
})
}
You can reduce your fieldArray to build key value pair for your filter
Example:
// create a filterMap for your fields and value that you want to filter with
var filterMap = {
sex: "gents",
brand: "brandName"
};
var fieldsArray = ["sex", "brand", "productNameNl", "eanCode"];
var getBrandGentsProducts = getProducts(filterArray);
function mapFilter(fields, filter) {
return fieldsArray.reduce(function (obj, field) {
if (filterMap[field]) {
obj[field] = filterMap[field];
}
return obj;
}, {});
}
function getProducts(fields, filter){
var filterPair = mapFilter(fields, filter);
//console.log(filterPair) //output { "brand": "brandName", "sex": "gents" }
Product.Find(filterPair).exec(function(err, products){
return products;
})
}
I want to create a new nested array with the name of a variable. Is this possible?
data example:
[{
varname {
name: person1name;
events : [{
firstName: person1name,
name: event1name,
date: date1
},{
firstName: person2name,
name: event2name,
date: date2
}
]
}
}]
code:
$scope.multiEvents = []
var eventCategory = $scope.event.name
$scope.multiEvents.eventCategory.name = $scope.event.firstName;
$scope.multiEvents.eventCategory.events.push($scope.event);
$scope.multiEvents.eventCategory.events.push($scope.event2);
Ended up doing this:
$scope.multiEvents = [];
$scope.multiEvent = {};
$scope.multiEvent.events = [];
$scope.multiEvent.name = $scope.event.firstName;
$scope.multiEvent.events.push($scope.event);
$scope.multiEvent.events.push($scope.event2);
$scope.multiEvents.push($scope.multiEvent);
I think I was just confused.