reggie
reggie

Reputation: 3674

Creating multiple database entries with node-orm2 in NodeJS

I want to save several objects into a database that I have defined in my NodeJS (express) app. The database module is node-orm2 and the database is an SQLite.

var record;

record = {
    'title' : 'Title 1'
};
req.models.myModel.create(record, function (err, item) {
    console.log('row id: ' + item.id);
});

record = {
    'title' : 'Title 2'
};
req.models.myModel.create(record, function (err, item) {
    console.log('row id: ' + item.id);
});

record = {
    'title' : 'Title 3'
};
req.models.myModel.create(record, function (err, item) {
    console.log('row id: ' + item.id);
});

The output is:

row id: 3
row id: 3
row id: 3

But I expect it to be like this (or in a different order):

row id: 1
row id: 2
row id: 3

What am I doing wrong? I tried to create the models in other ways, and I also tried the save method. I get the same results.

When I look at the database, I see that the three rows were added with the expected ids (1,2,3). So why is node-orm2 not returning the right id? I need to work with this ids to add more objects to the database.

Upvotes: 0

Views: 296

Answers (1)

jalooc
jalooc

Reputation: 1227

Try adding them in array instead of calling create() for each one: https://github.com/dresende/node-orm2#modelcreateitems-cb

Upvotes: 1

Related Questions