使用node/express構建API。我遇到問題使用mongoDB更新集合中的數組。我只想將引用ID添加到集合用戶中的useritems數組中。更新mongodDB集合中的數組
用戶看起來是這樣的:
[
{
fbname: "name.name",
email: "[email protected]",
username: "mr.name",
useritems: [ObjectId("51e101df2914931e7f000003"), ObjectId("51e101df2914931e7f000005"), ObjectId("51cd42ba9007d30000000001")]
}];
在我server.js:
var express = require('express'),
item = require('./routes/items');
var app = express();
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.bodyParser());
});
app.put('/users/:userId/item/:itemId/add', item.addUserItem);
查詢:
exports.addUserItem = function(req, res) {
var user = req.params.userId;
var item = req.params.itemId;
console.log('Updating useritem for user: ' + user);
console.log("item: ", item);
db.collection('users', function(err, collection) {
collection.update({'_id':new BSON.ObjectID(user)}, {$addToSet: { useritems: item}});
});
}
當我嘗試它捲曲什麼也沒有發生,我也得到了錯誤:(52)來自服務器的空回覆,來自捲曲
捲曲:
curl -i -X PUT -H 'Content-Type: applicatio/json' http://127.0.0.1:3000/users/51e6a1116074a10c9c000007/item/51e6a1116074a10c9c000006/add
但它更新集合? – dcodesmith