Azure mobile apps CRUD operations on SQL table (node.js backend)

会有一股神秘感。 提交于 2019-12-07 12:43:05

问题


This is my first post here so please don't get mad if my formatting is a bit off ;-) I'm trying to develop a backend solution using Azure mobile apps and node.js for server side scripts. It is a steep curve as I am new to javaScript and node.js coming from the embedded world. What I have made is a custom API that can add users to a MSSQL table, which is working fine using the tables object. However, I also need to be able to delete users from the same table. My code for adding a user is:

var userTable = req.azureMobile.tables('MyfUserInfo');
item.id = uuid.v4();

userTable.insert(item).then( function (){
    console.log("inserted data");
    res.status(200).send(item);
});

It works. The Azure node.js documentation is really not in good shape and I keep searching for good example on how to do simple things. Pretty annoying and time consuming. The SDK documentation on delete operations says it works the same way as read, but that is not true. Or I am dumb as a wet door. My code for deleting looks like this - it results in exception

query = queries.create('MyfUserInfo')
        .where({ id: results[i].id });

userTable.delete(query).then( function(delet){
console.log("deleted id ", delet);
});

I have also tried this and no success either

userTable.where({ id: item.id }).read()
        .then( function(results) {
        if (results.length > 0) 
        {
            for (var i = 0; i < results.length; i++)
            {
                userTable.delete(results[i].id);
                });
            }
        }

Can somebody please point me in the right direction on the correct syntax for this and explain why it has to be so difficult doing basic stuff here ;-) It seems like there are many ways of doing the exact same thing, which really confuses me. Thanks alot Martin


回答1:


You could issue SQL in your api

var api = {
get: (request, response, next) => {
    var query = {
        sql: 'UPDATE TodoItem SET complete=@completed',
        parameters: [
            { name: 'completed', value: request.params.completed }
        ]
    };

    request.azureMobile.data.execute(query)
    .then(function (results) {
        response.json(results);
    });
}};
module.exports = api;

That is from their sample on GitHub Here is the full list of samples to take a look at




回答2:


Why are you doing a custom API for a table? Just define the table within the tables directory and add any custom authorization / authentication.



来源:https://stackoverflow.com/questions/35998562/azure-mobile-apps-crud-operations-on-sql-table-node-js-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!