我有两个mongoDB集合(玩家和对手)。我想将它们的内容发送到同一路由,以便可以在不同的上下文中使用这些值。
我正在使用以下代码,但收到错误allPlayers is not defined
。我怀疑更大的问题是,无法像我一样使用create
将两个数据集发送到res.render
。
通常,我将在错误处理之后只使用else {res.render (create,{allPlayers:players})}
,但是如果我还想发送第二个数据集,显然不能这样做。
我是新来的快递/蒙哥等人,所以可能犯了一个愚蠢的错误,但任何帮助都将不胜感激。
app.get("/create", function(req,res){
Player.find({}, function(err, allPlayers){
if (err){
console.log("There is an error")
}
});
Opponent.find({}, function(err, allOpponents){
if (err){
console.log("There is an error")
}
});
res.render("create", {players: allPlayers, opponents: allOpponents});
});
答案 0 :(得分:1)
对您的代码执行以下操作。这应该可以解决您的问题(使用最新语法)。未定义“ allPlayers”,因为您在回调范围之外引用了它。
package com.example.gayathrikasilingam.classy01;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class RegActivity extends AppCompatActivity {
private TextInputLayout mName;
private TextInputLayout mEmail;
private TextInputLayout mPwd;
private Button mSubmit;
String username, email,password;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
mAuth = FirebaseAuth.getInstance();
mName=(TextInputLayout)findViewById(R.id.pName);
mEmail=(TextInputLayout)findViewById(R.id.pEmail);
mPwd=(TextInputLayout)findViewById(R.id.pPassword);
mSubmit=(Button) findViewById(R.id.regStart);
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username= mName.getEditText().getText().toString();
email= mEmail.getEditText().getText().toString();
password= mPwd.getEditText().getText().toString();
Toast.makeText(getApplicationContext(), username+" "+email+" "+password,
Toast.LENGTH_SHORT).show();
register_user(username,email,password);
}
});
}
private void register_user(String username, String email, String password)
{
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult () {
@Override
public void onComplete(@NonNull Task<AuthResult task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("yaaayy", "createUserWithEmail:success");
Intent i= new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
finish();
} else {
// If sign in fails, display a message to the user.
Log.w("oops", "createUserWithEmail:failure", task.getException());
Toast.makeText(getApplicationContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}
答案 1 :(得分:0)
请尝试以下代码:
app.get("/create", (req,res) => {
Player.find({}, function(err, allPlayers){
if (err){
console.log("There is an error")
}
else{
Opponent.find({}, function(err, allOpponents){
if (err){
console.log("There is an error")
}
else
res.render("create", {players: allPlayers, opponents: allOpponents});
});
}
});
});
在执行res.render
之前,您应该等待两个查询都返回结果。
我已经在callbacks
中添加了查询。
让我知道是否有帮助。