Reputation: 861
I am using Azure Mobile Services with Easy Tables and am looking to prevent duplicate entries being inserted into one of my tables based on a specific column (name). I understand that the Primary Key has to be on the Id column so am looking to change the javascript file on Azure to check if the data already exists in that column and prevent a new record if so.
This is what I have so far:
Table.js
var table = module.exports = require('azure-mobile-apps').table();
table.insert(function (context) {
// Check for duplicate on name column
});
Upvotes: 0
Views: 111
Reputation: 1701
Try the following:
var table = module.exports = require('azure-mobile-apps').table();
table.insert(function (context) {
return table.read({ name: context.item.name }).then(function (results) {
if(results.length > 0)
context.res.status(400).send("A record with that name already exists");
else
return context.execute();
});
});
Upvotes: 3