0

I still struggling how can I create and store particular size of objects in sequelize.

Here is my code:

var pointsize = req.body.pointsize;

Point.bulkCreate([{ pointname: req.body.point }]).success(function() {});

I wanna store pointsize count of rows in Point table. Thanks for any advise guys!

Edited:

 function makeArrayOf(value, length) {
   var arr = [], i = length;
   while (i--) {
     arr[i] = value + (i+1);
   }
   return arr;
 }
 var pointsize = req.body.pointsize;

 Point.bulkCreate([makeArrayOf('Point Name', pointsize)]).success(function() { });

something like this... This create me pointsize rows in db (As I wish). But I don't know how to store name of point into database.

So if pointsize = 5 then rows in database will looks like:

id |   name  | ownerid
---------------------
1  | Point 1 | 101
2  | Point 2 | 101
3  | Point 3 | 101
4  | Point 4 | 101
5  | Point 5 | 101
5
  • Does the Point model have a pointsize column? In that case it should be as simple as adding pointsize: whatever count you want when creating. Otherwise, please explain more thoroughly what you wish to do - show tables or model definitions Commented Nov 20, 2014 at 12:54
  • @JanAagaardMeier Thanks for help! I have edited code please take a look. Commented Nov 20, 2014 at 22:31
  • I'm still not sure what you want to do? What exactly do you want a row in your table to look like? If you simply wish to insert a row with to columns, you can do bulkCreate([{ name: 'sometihng', pointsize: 42 }]). Commented Nov 21, 2014 at 7:08
  • @JanAagaardMeier Pointsize is value from my form (If somebody will add number 10 then post function will create 10 rows in point table with names Point1,Point2,Point3,Point4....Point10). Commented Nov 21, 2014 at 11:14
  • @JanAagaardMeier Please take a look, now it is clear enough. Commented Nov 21, 2014 at 11:56

1 Answer 1

1
function makeArrayOf(value, length) {
   var arr = [], i = length;
   while (i--) {
     arr[i] = { name: value + (i+1) };
   }
   return arr;
}
var pointsize = req.body.pointsize;

Point.bulkCreate(makeArrayOf('Point Name', pointsize)).success(function() { });

Notice how the value has been wrapped in an object, and I removed the array aroud the return value of makeArrayOf.

Each element in the arrray will be an object, with a property name:

Point.bulkCreate([{ name:'..' }, {name: '..'}, ...]).success(function() { });
Sign up to request clarification or add additional context in comments.

2 Comments

BTW, what is different between success and then? Sequelize documentation uses then, but I found the success usage every now and then. What is the correct and format one?
With v 2.0 sequelize switched from eventemitters to promises. success is event emitter style, then is promise style

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.