mam tego schematu Mongooseprzesuwając obiekt w schemacie tablicy w Mongoose
var mongoose = require('mongoose');
var ContactSchema = module.exports = new mongoose.Schema({
name: {
type: String,
required: true
},
phone: {
type: Number,
required: true,
index: {unique: true}
},
messages: [
{
title: {type: String, required: true},
msg: {type: String, required: true}
}]
}, {
collection: 'contacts',
safe: true
});
i próby aktualizacji modelu w ten sposób:
Contact.findById(id, function(err, info) {
if (err) return res.send("contact create error: " + err);
// add the message to the contacts messages
Contact.update({_id: info._id}, {$push: {"messages": {title: title, msg: msg}}}, function(err, numAffected, rawResponse) {
if (err) return res.send("contact addMsg error: " + err);
console.log('The number of updated documents was %d', numAffected);
console.log('The raw response from Mongo was ', rawResponse);
});
});
jestem ja nie uznająca messages
wziąć tablica obiektów?
BŁĄD: MongoError: Nie można stosować $ Push/$ pushAll modyfikator do braku tablicy
Jakieś pomysły?
Jaki komunikat błędu otrzymujesz? Co masz na myśli, mówiąc: "Nie wydaje mi się, żeby" pchnięcie w dół "zostało wykonane właściwie? – shelman
MongoError: Nie można zastosować modyfikatora $ push/$ pushAll do tablicy innej niż – user1460015
Nie wiem, czy to rozwiąże problem, ale spróbuj odjąć nawiasy kwadratowe od '[{title: title, msgs: [msg]}]'. '$ push' przyjmuje jedną wartość. – shelman