在findById()之后,Mongoose save()无法正常工作

时间:2017-06-09 09:24:13

标签: node.js mongoose

给出汽车的以下架构/结构:

car : {
    _id: "123123",
    name: "Ferrari",
    owners: [
        {
            _id : "098098",
            active: true
        },
        {
            _id : "876876",
            active: true
        }
    ]
}

我想将所有owner.active更改为false

使用以下代码我可以保存更新的汽车对象,但它不会保存到数据库中:

// find car by id
db_car.findById(req.id).then(function(car){
  car.owners.forEach(function(owner){
    owner.active = false;
  });
  car.save();
  console.log('updated car', car); // this is correct, shows updated data
});

//现在检查数据库,数据没有保存在数据库中!

我正在使用Node.js,MongoDB和Mongoose。

1 个答案:

答案 0 :(得分:0)

Mongoose .findById()会返回一个查询。 Mongoose查询是承诺。相反,您可以使用.exec()执行查询,而db_car.findById(req.id).exec().then(function(car){ ... 又会返回一个承诺。例如:

PeerConnectionFactory.initializeAndroidGlobals(this, true);
final PeerConnectionFactory pcFactory = new PeerConnectionFactory(null);
final PeerConnection pc = pcFactory.createPeerConnection(ICE_SERVERS, MEDIA_CONSTRAINTS, new PeerConnection.Observer() {
    private final String TAG = "WebRTC";

    @Override
    public void onSignalingChange(final PeerConnection.SignalingState signalingState) {
        Log.d(TAG, "Signaling change\t" + signalingState);
    }

    @Override
    public void onIceConnectionChange(final PeerConnection.IceConnectionState iceConnectionState) {
        Log.d(TAG, "ICE connection change\t" + iceConnectionState);
    }

    @Override
    public void onIceConnectionReceivingChange(final boolean b) {
        Log.d(TAG, "ICE connection receiving change\t" + b);
    }

    @Override
    public void onIceGatheringChange(final PeerConnection.IceGatheringState iceGatheringState) {
        Log.d(TAG, "ICE gathering change\t" + iceGatheringState);
    }

    @Override
    public void onIceCandidate(final IceCandidate iceCandidate) {
        Log.d(TAG, "ICE candidate\t" + iceCandidate);
    }

    @Override
    public void onIceCandidatesRemoved(final IceCandidate[] iceCandidates) {
        for (final IceCandidate iceCandidate : iceCandidates){
            Log.d(TAG, "ICE candidate removed\t" + iceCandidate);
        }
    }

    @Override
    public void onAddStream(final MediaStream mediaStream) {
        Log.d(TAG, "Add stream\t" + mediaStream);
    }

    @Override
    public void onRemoveStream(final MediaStream mediaStream) {
        Log.d(TAG, "Remove stream\t" + mediaStream);
    }

    @Override
    public void onDataChannel(final DataChannel dataChannel) {
        Log.d(TAG, "Data channel\t" + dataChannel);
    }

    @Override
    public void onRenegotiationNeeded() {
        Log.d(TAG, "Renegotiation needed");
    }

    @Override
    public void onAddTrack(final RtpReceiver rtpReceiver, final MediaStream[] mediaStreams) {
        for (final MediaStream mediaStream : mediaStreams) {
            Log.d(TAG, "Add track\t" + rtpReceiver + "\t" + mediaStream);
        }
    }
});
final AudioSource audioSource = pcFactory.createAudioSource(MEDIA_CONSTRAINTS);
Assert.assertNotNull(audioSource);
final AudioTrack audioTrack = pcFactory.createAudioTrack("Test Audio Track", audioSource);
Assert.assertNotNull(audioTrack);
final MediaStream localMS = pcFactory.createLocalMediaStream("Test Local Media Stream");
Assert.assertNotNull(localMS);
Assert.assertTrue(localMS.addTrack(audioTrack));
Assert.assertTrue(pc.addStream(localMS));
pc.createOffer(new SdpObserver() {
    private final String TAG = "WebRTC";

    @Override
    public void onCreateSuccess(final SessionDescription sessionDescription) {
        Log.d(TAG, "Create success\t" + sessionDescription.type + " " + sessionDescription.description);
        pc.setLocalDescription(this, sessionDescription);
    }

    @Override
    public void onSetSuccess() {
        Log.d(TAG, "Set success");
    }

    @Override
    public void onCreateFailure(final String s) {
        Log.d(TAG, "Create failure\t" + s);
    }

    @Override
    public void onSetFailure(final String s) {
        Log.d(TAG, "Set failure\t" + s);
    }
}, MEDIA_CONSTRAINTS);

相关文件:http://mongoosejs.com/docs/promises.html#queries-are-not-promises