我正在扩展在NodeJS,Angular,MongoDB和sails框架中构建的现有自定义应用程序。我正在为用户订购三明治。
当用户进行选择时,会创建一个存储订单详细信息的javascript对象。该对象应该被添加到数据库中的现有“购物车”集合中。
问题在于,当javascript对象被添加到“cart”集合时,它会创建一个非常奇怪的文档,它根本不是应该存在的数据。
以下代码基于成功更新购物车的现有代码。现有的代码是我继承的垃圾箱火,而且由于我还在学习,调试这个问题一直很困难。
以下是用户填写表单时创建的对象:
//boxlunch object
{"boxlunchType":"Standard Traditional","sandwichesType":"sandwich","sandwichesTotalQty":8,"totalPrice":110,"sandwiches":[{"name":"Chicken Salad","qty":1},{"name":"Grilled Chicken Breast","qty":2},{"name":"Grilled Vegetables Provolone","qty":""},{"name":"Ham and Swiss Cheese","qty":""},{"name":"Roast Beef","qty":5}]}
然后将对象传递给此函数,该函数创建一个简单的数组。数据将传递到执行数据库更新的路由。
/*Save boxlunch cart*/
$scope.saveCartBoxlunch = function(boxlunch) {
var boxlunchCart = [];
boxlunchCart.push(
{
'quantity' : boxlunch.sandwichesTotalQty,
'unitprice' : boxlunch.totalPrice,
'customer_id' : $rootScope.user.displayname,
'item_name' : boxlunch.boxlunchType
}
);
$sails.post('/boxlunchcart?boxlunch=' + boxlunchCart).then(function (data) {
});
};
这是“boxlunchcart”路线调用的函数。
boxlunchUpdateCart : function (req, res) {
var boxlunchCartJSON = req.param('boxlunch');
Cart
.find(
{
customer_id:req.session.user[0].displayname
}
)
.exec(function(err, cartchKObj) {
if (err) {
sails.log.error('Error finding cart data',err);
res.send(500, {title: 'Error finding cart data'});
//If cart does not exist create new
} else if (cartchKObj == '' || typeof cartchKObj == 'undefined') {
Cart
.create(boxlunchCartJSON)
.exec(function (err, cartObj){
sails.log.debug("Cart object: " + cartObj.id);
if(err){
res.send(500, {title: 'error Creating Boxlunch Cart'});
sails.log.error('Error Creating Boxlunch Cart',err);
} else {
sails.log.error('Cart Created Successfully',cartObj);
}
});
}
//If cart exists update existing
else {
sails.log.debug("cartchKObj " + cartchKObj[0].id);
Cart
.update( {id : cartchKObj[0].id}, {quantity : 1})
.exec(function(err, cartUpdateObj) {
sails.log.debug("Cart update object: " + cartUpdateObj.id);
if (err) {
sails.log.error('Error Updating Boxlunch Cart',err);
res.send(500, {title: 'error Updating Boxlunch Cart'});
} else {
sails.log.debug('Boxlunch Cart Updated Successfully');
}
});
}
});
这是所有sails.log消息的输出。
如您所见,此行打印已创建的新文档ObjectID。
sails.log.debug("Cart object: " + cartObj.id);
//Output
{"level":"debug","message":"Cart object: 599b63483d6966050026189b","timestamp":"2017-08-21T22:48:40.728Z"}
此行打印已创建的新对象
sails.log.debug('Cart Created Successfully',cartObj);
//Output (added returns for readability)
{"level":"debug","message":"Cart Created Successfully
{ '0': '[',\n '1': 'o',\n '2': 'b',\n '3': 'j',\n '4': 'e',\n '5': 'c',\n '6': 't',\n '7': ' ',\n '8': 'O',\n '9': 'b',\n '10': 'j',\n '11': 'e',\n '12': 'c',\n '13': 't',\n '14': ']',\n bold: '\\u001b[1m[object Object]\\u001b[22m',\n
underline: '\\u001b[4m[object Object]\\u001b[24m',\n strikethrough: '\\u001b[9m[object Object]\\u001b[29m',\n italic: '\\u001b[3m[object Object]\\u001b[23m',\n inverse: '\\u001b[7m[object Object]\\u001b[27m',\n grey: '\\u001b[90m[object Object]\\u001b[39m',\n black: '\\u001b[30m[object Object]\\u001b[39m',\n yellow: '\\u001b[33m[object Object]\\u001b[39m',\n red: '\\u001b[31m[object Object]\\u001b[39m',\n green: '\\u001b[32m[object Object]\\u001b[39m',\n blue: '\\u001b[34m[object Object]\\u001b[39m',\n white: '\\u001b[37m[object Object]\\u001b[39m',\n cyan: '\\u001b[36m[object Object]\\u001b[39m',\n
magenta: '\\u001b[35m[object Object]\\u001b[39m',\n greyBG: '\\u001b[49;5;8m[object Object]\\u001b[49m',\n blackBG: '\\u001b[40m[object Object]\\u001b[49m',\n yellowBG: '\\u001b[43m[object Object]\\u001b[49m',\n redBG: '\\u001b[41m[object Object]\\u001b[49m',\n greenBG: '\\u001b[42m[object Object]\\u001b[49m',\n blueBG: '\\u001b[44m[object Object]\\u001b[49m',\n whiteBG: '\\u001b[47m[object Object]\\u001b[49m',\n cyanBG: '\\u001b[46m[object Object]\\u001b[49m',\n magentaBG: '\\u001b[45m[object Object]\\u001b[49m',\n
rainbow: '\\u001b[31m[\\u001b[39m\\u001b[33mo\\u001b[39m\\u001b[32mb\\u001b[39m\\u001b[34mj\\u001b[39m\\u001b[35me\\u001b[39m\\u001b[31mc\\u001b[39m\\u001b[33mt\\u001b[39m \\u001b[34mO\\u001b[39m\\u001b[35mb\\u001b[39m\\u001b[31mj\\u001b[39m\\u001b[33me\\u001b[39m\\u001b[32mc\\u001b[39m\\u001b[34mt\\u001b[39m\\u001b[35m]\\u001b[39m',\n zebra: '[\\u001b[7mo\\u001b[27mb\\u001b[7mj\\u001b[27me\\u001b[7mc\\u001b[27mt\\u001b[7m \\u001b[27mO\\u001b[7mb\\u001b[27mj\\u001b[7me\\u001b[27mc\\u001b[7mt\\u001b[27m]',\n stripColors: '[object Object]',\n
zalgo: '[̶̮̠̥͈̭̣͎̲̙͇̞̹̘͈̺͉̣̺͍̟͍̦̻̙͔͎̙̘̯̙̥̖̺̱̥̮̬̖̹͎̤̠̗̮̭̙̼̻̼̪̙͉͖̥̞̺̰̲͕͕̠͖̥̉̅̓̇̄ͥͅͅo̷͔̺͈̦̩͇̞̗̣͙̘̬̩̟̫̮̙̜̞̤̺̥͖̯̗̦̞͖̣̳̺̠̥̳̲̣̰̹͖̳̝͚̜̦̥̺͙̣̱̮͉̘̼͈̦̱̞͎͔̱̮̤̤̝͇̠ͦ̽̐͗͒̇̉ͩ͆̐b̞̮͓̭͔̭͈̗̪̤͓͎̰̟͙̤̳̦͇̯̖̣̺͉̠̹̱̪̱̮̙̝͖͇͈̲̘͚͙̻̬͍̩̥̭̲̫̬̲͚̱̟͙̂̒͊̐́̄ͪ̎̒ͥ͑̓͑͂ͯ͠ͅjͨ͋̈ͤ̅̊̚ ҉̰̝̣̮̘̘͉̗̣̥̮͚̭̹̠̥̖̰̟̠̹̜̙̜̻̠̥͍̩̟̳̞̩͉͙̪̖̥̣̩͍͍̜͉͈̣͉̗̝͙͚̘̰͕͉̠̮̩͎̯̖ͅͅe̡̬̤̙̦̼̟͍̘̖̥̜̗̭͈͕͍̞̞̯͇̞̙͔̻̫͓̞̱̭̞̲̊ͮͦ̽̓̽ͤ̒́̍ͪͣͨ̏̚c̟̥̘̤̙̮̺̝͍͍̺̙͍͉̞͍̥̻̪̖̦̳͇̼̫̲̝͙͍̦̖̫̗͙̞͔̻̖̙͈̘̱̩͇̟̻̫̞̮̯̮̰͚͉̞̒͆̌̐̐̾ͩ́̽̾͐͛̐ͯ͗̓̉͘t͗ͤ͆̆͐ͯͧͬ̋ͧ͑ͬͦ̀ͯ̄͋ ҉̺͔̳̟̣̰̙̳̗̗̻̣͍̥̟͓̯͍͕̣͙̱̘̱̰̱͉̤͚̣̠̱̤̞̗̟͔̲̦̤̲̘̬̺̞͔̯̹̝̥̱͇̪̲ͅͅͅͅ ͇̟̠̗͚͔͍̜̟̲̪̳̻̟̭̬̟̝̠̱̥̫̥̳̳͚͕̣͈̼̼̻̼̱̖͙̭͚͓̗͍̥̞̟̀̆ͥ͒̈ͦ̔͋͝ͅO̜̹̮̭͇͎͓̘̺̹̩̱͚̦̯̞̣̣̼̲͖̱̺̠͙͙̹͉̘̻̖̥͓͍̦̰͈̺̮̜̺̝̭̜̗̺̞̣͕̞̤ͦ̌͊̑̅͆͞ͅb̵̲͎̮̦̣̠͎̤͕̬͇̞̹̞̖̪̦͔͍̞̦̖̘̠̙̯̞̲͈̟̻̰̦̭̼̳̲̩͇̫̬̤̰̃͐̊̄j̴̼͉͇̲̺̺̥̣̱͎̭̫̖͎̖̬̺̞̭͓̥̙̻̜̠͍̤̖̩̙̻̦̪̝̣̣̫̝̩͉̥̯̘̗̯̰͉͉͈̭̅ͯ̓̐̇͒̂̔̊͐ͩͣ̅̿ě̷̦͔͈͙̥͈͙̱͚̠͖̣̺̰̘̝̝̤̱̞̗̮̘̭̬͔̼̟̮͎̱̼̬̙̠̤̲͓̠͉̹̼̫͈̘̯͕̗͚̲͓̼͖̺͍̠͙͍̩̓ͪ̔ͮͅc̛͍͈̝͔̰̹͉̖͚̝̻̆̊̅͆ț̨̼̯̟͇̜͇̱̟̝̫͕̩͎̠̤̯͎̘̳͇̳̞̼ͪ͋ͣ͌ͭͣ̍͌ͧ̽͑̉ͦ̌̎ͫͣ]̡̩͈͍͇̪͈̫ͮͮ͆ͧ͛ͫ̎̄̇̊ͫ͒̓̔ͥ̊́̾̑',\n
silly: '\\u001b[31m[\\u001b[39m\\u001b[33mo\\u001b[39m\\u001b[32mb\\u001b[39m\\u001b[34mj\\u001b[39m\\u001b[35me\\u001b[39m\\u001b[31mc\\u001b[39m\\u001b[33mt\\u001b[39m \\u001b[34mO\\u001b[39m\\u001b[35mb\\u001b[39m\\u001b[31mj\\u001b[39m\\u001b[33me\\u001b[39m\\u001b[32mc\\u001b[39m\\u001b[34mt\\u001b[39m\\u001b[35m]\\u001b[39m',\n input: '\\u001b[30m[object Object]\\u001b[39m',\n verbose: '\\u001b[36m[object Object]\\u001b[39m',\n prompt: '\\u001b[90m[object Object]\\u001b[39m',\n info: '\\u001b[32m[object Object]\\u001b[39m',\n blank: '\\u001b[37m[object Object]\\u001b[39m',\n
data: '\\u001b[90m[object Object]\\u001b[39m',\n help: '\\u001b[36m[object Object]\\u001b[39m',\n
warn: '\\u001b[33m[object Object]\\u001b[39m',\n
debug: '\\u001b[34m[object Object]\\u001b[39m',\n
error: '\\u001b[31m[object Object]\\u001b[39m',\n crit: '\\u001b[31m[object Object]\\u001b[39m',\n id: '599b63483d6966050026189b' }","timestamp":"2017-08-21T22:48:40.730Z"}
问题:如何修复此代码以使数据正确创建所需文档?
更新:也许我需要将javascript对象转换为JSON对象?
答案 0 :(得分:0)
我已经解决了这个问题,这是因为对象是作为请求参数发送的,而不是在请求体中发送的。我认为当它作为参数发送时,一定存在某种编码问题。经验教训!
以下是更新:
$sails.post('/boxlunchcart', boxlunchCart).then(function (data)
var boxlunchCartJSON = req.body;