I have a mongoose schema model that looks like the below example. The accounts field is an array of account IDs.
var ListingSchema = new Schema({
listing: { type: String, required: true },
admin: { type: String, required: true, index: true },
accounts: [{ type: String, index: true }]
});
Now in my Node/Express backend, I add an entry like so:
Listing.create({
listing: req.body.id,
admin: req.session.userID,
account: req.session.userID
}, function (err){
if (err) throw err;
else res.send({success:true});
});
However I'm not adding to the account field correctly because it's an array. How can I do that?
For a create try this:
var accounts = [req.session.userID];
Listing.create({
listing: req.body.id,
admin: req.session.userID,
accounts: accounts
}, function (err){
if (err) throw err;
else res.send({success:true});
});
For an update try this:
{ $push: { accounts: 'value' }
var accounts = [req.session.userID];
And
accounts: [{ type: String, index: true }]
should be
accounts: [{ type: Array, index: true }]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.